FU
DOCUMENT

Functional Programming with purrr Cheat Sheet

A purrr reference for map functions, input and output types, error handling and data-frame composition in repeated R workflows.

Version 2026-08-22通用Public reference material; verify the included notice and project terms before redistribution

What this reference covers

purrr expresses the operation of applying one function to each element of a vector, list, data frame row or file. This reference covers map variants, type-stable output, parallel inputs, list columns, result binding and errors for repeated workflows.

Define the single-item contract

Write and test a function for one input before mapping over a collection. State the expected return type, names and failure behavior. Lists can preserve complex objects, while vectors or row-bound data frames need compatible results from every item.

Preserve failures and input identity

For files, networks or databases, keep input identifiers, status and retry records. Wrap potentially failing work so one bad item returns a structured error result, then inspect failures together instead of silently dropping them.

Maintenance note

Mapping repeats the function supplied to it; it does not prove that the function is correct. Randomness, side effects, I/O, concurrency and permissions need explicit order and logging. Content review date: 2026-08-23.

SAVE TO CLOUD

Save to your cloud drive

Save the complete collection first so files remain together and are easier to access across devices.

Links checked 2026-08-06
Save first, access when you need itOn desktop, scan with the matching cloud-drive app. On mobile, tap the save button.
GUIDE

purrr iteration study guide

Test a single-item function first, choose a type-stable map variant, combine compatible results and preserve failures with their input identifiers.

Before you start

  • Know R vectors, lists, functions and data frames.
  • Prepare a small non-sensitive practice data set or file list.
  • Decide the output type and failure behavior for one item.
02

Quick start

  1. 01

    Identify the iteration object

    Decide whether you are traversing a vector, list, row, group or file name and state what one element should return.

  2. 02

    Choose a map variant

    Select list, logical, integer, character or row-binding output based on the contract instead of creating a mixed list and fixing types later.

  3. 03

    Combine structured results

    Use parallel mapping or binding when inputs return compatible fields, preserving input names and failure locations.

  4. 04

    Add error handling

    Return identifiable error objects for failed items, inspect the complete failure list and decide on retries after the batch finishes.

Usage tips

  • Test the pure single-item function with successful and boundary examples before mapping.
  • Type-stable outputs simplify data-frame binding, testing and serialization.
  • For external I/O, keep identifiers, retry decisions, logs and permission checks with each result.
Troubleshooting and uninstall

Why do map results have inconsistent types?

Inspect every branch of the single-item function, define success and failure structures and use a type-stable map or wrapper.

Why does one item stop the whole batch?

Wrap the item operation so it returns an error object with input identity, then review and retry failures after the batch.

FAQ

Frequently asked questions

What problem does purrr solve?

It makes repeated application of one function to vectors, lists or rows explicit and helps control output types, names and errors.

How do map and map_dfr differ?

map returns a list, while map_dfr is suited to row-binding compatible data frames returned by each item.

When might functional iteration be a poor fit?

Evaluate ordinary loops or specialized vectorized tools when work has complex shared state, strict sequencing or a single optimized computation.

How can failed items be preserved?

Return a structured result with input identifier, status and error details, then filter failures and choose a retry policy after processing.