fapply: Apply a function over a list or vector with optional...

View source: R/fapply.R

fapplyR Documentation

Apply a function over a list or vector with optional parallelism and progress

Description

A lightweight and fast version of 'lapply()' with support for multicore (Unix) and snow-style clusters via 'parallel', with internal progress bar tracking and message suppression.

Usage

fapply(.x, .f, ncores = 1, pb = FALSE, cl = NULL, load_balancing = TRUE, ...)

Arguments

.x

A list or atomic vector.

.f

Function to apply.

ncores

Number of cores to use (default: 1 = sequential).

pb

Show progress bar? (default: FALSE).

cl

A cluster object (from parallel::makeCluster), or integer for core count.

load_balancing

Logical. Use 'parLapplyLB' if 'TRUE' (default: 'FALSE').

...

Additional arguments passed to '.f'.

Value

A list of results.

Examples

# Basic usage (sequential)
fapply(1:5, sqrt)

# With progress bar (sequential)
fapply(1:5, function(x) { Sys.sleep(0.1); x^2 }, pb = TRUE)

# Multicore on Unix (if available)

if (.Platform$OS.type != "windows") {
  fapply(1:10, sqrt, ncores = 2)
}


# With user-created cluster (portable across platforms)

cl <- parallel::makeCluster(2)
fapply(1:10, sqrt, cl = cl)
parallel::stopCluster(cl)


# Heavy computation example with chunked parallelism

heavy_fn <- function(x) { Sys.sleep(0.05); x^2 }
fapply(1:20, heavy_fn, ncores = 2, pb = TRUE)



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