fmapn: Apply a function over multiple argument lists in parallel

View source: R/fmapn.R

fmapnR Documentation

Apply a function over multiple argument lists in parallel

Description

Applies a function '.f' over multiple aligned lists in '.l'. Each element of '.l' should be a list or vector of the same length. Each call to '.f' receives one element from each list. Supports parallel execution and progress display.

Usage

fmapn(.l, .f, ncores = NULL, pb = FALSE, ...)

Arguments

.l

A list of vectors or lists. All elements must be of equal length.

.f

A function to apply. It must accept as many arguments as there are elements in '.l'.

ncores

Integer. Number of cores to use for parallel processing. Default is 'NULL' (sequential).

pb

Logical. Whether to display a progress bar. Default is 'FALSE'.

...

Additional arguments passed to '.f'.

Value

A list of results obtained by applying '.f' to each tuple from '.l'.

Examples

# Fit a linear model for each response variable using the same predictor
df <- data.frame(
  y1 = rnorm(100),
  y2 = rnorm(100),
  x = rnorm(100)
)

# List of formulas and data
formulas <- list(y1 ~ x, y2 ~ x)
data_list <- list(df, df)

fmapn(list(formula = formulas, data = data_list), function(formula, data) {
  lm(formula, data = data)
})

# Extract model summaries in parallel
models <- fmapn(list(formula = formulas, data = data_list), function(formula, data) {
  summary(lm(formula, data = data))$r.squared
})


functionals documentation built on Aug. 8, 2025, 7:32 p.m.