furrr: Parallel purrr Operations
Drop-in replace map() with future_map() for instant parallelization.
furrr: Parallel purrr
furrr (future + purrr) provides drop-in parallel replacements for all purrr::map_*() functions. Simply swap map() for future_map() after setting a plan() and your pipeline runs in parallel with zero structural changes.
library(furrr)
library(future)
# Set up parallel workers
plan(multisession, workers = 4)
# Sequential (purrr)
# result <- purrr::map(1:8, ~.x^2)
# Parallel (furrr) — identical API
result <- future_map(1:8, ~.x^2)
cat(unlist(result), '\n') # 1 4 9 16 25 36 49 64
plan(sequential)plan(multisession, workers = 4)
Specifying workers explicitly in plan() caps the number of parallel R sessions. For CPU-bound tasks, workers = parallel::detectCores() - 1 is a common convention to leave one core for the OS.
library(furrr)
library(future)
library(parallel)
# Explicit worker count
n_workers <- max(1, detectCores() - 1)
plan(multisession, workers = n_workers)
cat('Active workers:', nbrOfWorkers(), '\n')
cat('Strategy:', class(plan())[1], '\n')
# Run a simple parallel task
results <- future_map_dbl(1:8, ~sqrt(.x))
cat(round(results, 3), '\n')
plan(sequential)All lessons in this course
- parallel Package and detectCores()
- The future Framework
- furrr: Parallel purrr Operations
- Debugging and Load Balancing Parallel Code