Version: 0.1.0
Title: Parallelize Common Functions via One Magic Function
Description: The futurize() function transpiles calls to sequential map-reduce functions such as base::lapply(), purrr::map(), 'foreach::foreach() %do% { ... }' into concurrent alternatives, providing you with a simple, straightforward path to scalable parallel computing via the 'future' ecosystem <doi:10.32614/RJ-2021-048>. By combining this function with R's native pipe operator, you have an convenient way for speeding up iterative computations with minimal refactoring, e.g. 'lapply(xs, fcn) |> futurize()', 'purrr::map(xs, fcn) |> futurize()', and 'foreach::foreach(x = xs) %do% { fcn(x) } |> futurize()'. Other map-reduce packages that be "futurized" are 'BiocParallel', 'plyr', 'crossmap' packages. There is also support for growing set of domain-specific packages, including 'boot', 'glmnet', 'mgcv', 'lme4', and 'tm'.
License: GPL (≥ 3)
URL: https://www.futureverse.org, https://github.com/futureverse/
Depends: R (≥ 4.1.0), future (> 1.58.0)
Suggests: future.apply, foreach, doFuture, purrr, furrr, crossmap, plyr, BiocParallel, glmnet, boot, lme4, mgcv, tm, tools, commonmark, base64enc
VignetteBuilder: futurize
Language: en-US
Encoding: UTF-8
RoxygenNote: 7.3.3
NeedsCompilation: no
Packaged: 2026-01-18 16:58:50 UTC; hb
Author: Henrik Bengtsson [aut, cre, cph]
Maintainer: Henrik Bengtsson <henrikb@braju.com>
Repository: CRAN
Date/Publication: 2026-01-22 21:10:02 UTC

Turn common R function calls into concurrent calls for parallel evaluation

Description

logo

Usage

futurize(
  expr,
  substitute = TRUE,
  options = futurize_options(...),
  ...,
  when = TRUE,
  eval = TRUE,
  envir = parent.frame()
)

Arguments

expr

An R expression, typically a function call to futurize. If FALSE, then futurization is disabled, and if TRUE, it is re-enabled.

substitute

If TRUE, argument expr is substitute():d, otherwise not.

options, ...

Named options, passed to futurize_options(), controlling how futures are resolved.

when

If TRUE (default), the expression is futurized, otherwise not.

eval

If TRUE (default), the futurized expression is evaluated, otherwise it is returned.

envir

The environment from where global objects should be identified.

Value

Returns the value of the evaluated expression expr.

If expr is TRUE or FALSE, then a logical is returned indicating whether futurization was previously enabled or disabled.

Expression unwrapping

The transpilation mechanism includes logic to "unwrap" expressions enclosed in constructs such as { }, ⁠( )⁠, local(), I(), identity(), invisible(), suppressMessages(), and suppressWarnings(). The transpiler descends through wrapping constructs until it finds a transpilable expression, avoiding the need to place futurize() inside such constructs. This allows for patterns like:

y <- {
  lapply(xs, fcn)
} |> suppressMessages() |> futurize()

avoiding having to write:

y <- {
  lapply(xs, fcn) |> futurize()
} |> suppressMessages()

Conditional futurization

It is possible to control whether futurization should take place at run-time. For example,

y <- lapply(xs, fun) |> futurize(when = { length(xs) >= 10 })

will be futurized, unless length(xs) is less than ten, in which case it is evaluated as:

y <- lapply(xs, fun)

Disable and re-enable all futurization

It is possible to globally disable the effect of all futurize() calls by calling futurize(FALSE). The effect is as if futurize() was never applied. For example,

futurize(FALSE)
y <- lapply(xs, fun) |> futurize()

evaluates as:

y <- lapply(xs, fun)

To re-enable futurization, call futurize(TRUE). Please note that it is only the end-user that may control whether futurization should be disabled and enabled. A package must never disable or enable futurization.

Examples

xs <- list(1, 1:2, 1:2, 1:5)

# ------------------------------------------
# Base R apply functions
# ------------------------------------------
# Sequential lapply()
y <- lapply(X = xs, FUN = function(x) {
  sum(x)
})
   
# Parallel version
y <- lapply(X = xs, FUN = function(x) {
  sum(x)
}) |> futurize()
str(y)


# ------------------------------------------
# purrr map-reduce functions with pipes
# ------------------------------------------
if (require("purrr") && requireNamespace("furrr", quietly = TRUE)) {

# Sequential map()
y <- xs |> map(sum)
   
# Parallel version
y <- xs |> map(sum) |> futurize()
str(y)

} ## if (require ...)


# ------------------------------------------
# foreach map-reduce functions
# ------------------------------------------
if (require("foreach") && requireNamespace("doFuture", quietly = TRUE)) {

# Sequential foreach()
y <- foreach(x = xs) %do% {
  sum(x)
}
   
# Parallel version
y <- foreach(x = xs) %do% {
  sum(x)
} |> futurize()
str(y)


# Sequential times()
y <- times(3) %do% rnorm(1)
str(y)
   
# Parallel version
y <- times(3) %do% rnorm(1) |> futurize()
str(y)

} ## if (require ...)


# ------------------------------------------
# plyr map-reduce functions
# ------------------------------------------
if (require("plyr") && requireNamespace("doFuture", quietly = TRUE)) {

# Sequential llply()
y <- llply(xs, sum)
   
# Parallel version
y <- llply(xs, sum) |> futurize()
str(y)

} ## if (require ...)

Options for how futures are partitioned and resolved

Description

Options for how futures are partitioned and resolved

Usage

futurize_options(
  seed = FALSE,
  globals = TRUE,
  packages = NULL,
  stdout = TRUE,
  conditions = "condition",
  scheduling = 1,
  chunk_size = NULL,
  ...
)

Arguments

seed

(optional) If TRUE, the random seed, that is, the state of the random number generator (RNG) will be set such that statistically sound random numbers are produced (also during parallelization). If FALSE (default), it is assumed that the future expression neither needs nor uses random number generation. To use a fixed random seed, specify a L'Ecuyer-CMRG seed (seven integers) or a regular RNG seed (a single integer). If the latter, then a L'Ecuyer-CMRG seed will be automatically created based on the given seed. Furthermore, if FALSE, then the future will be monitored to make sure it does not use random numbers. If it does and depending on the value of option future.rng.onMisuse, the check is ignored, an informative warning, or error will be produced. If seed is NULL, then the effect is as with seed = FALSE but without the RNG check being performed.

globals

(optional) a logical, a character vector, or a named list to control how globals are handled. For details, see section 'Globals used by future expressions' in the help for future().

packages

(optional) a character vector specifying packages to be attached in the R environment evaluating the future.

stdout

If TRUE (default), then the standard output is captured, and re-outputted when value() is called. If FALSE, any output is silenced (by sinking it to the null device as it is outputted). Using stdout = structure(TRUE, drop = TRUE) causes the captured standard output to be dropped from the future object as soon as it has been relayed. This can help decrease the overall memory consumed by captured output across futures. Using stdout = NA fully avoids intercepting the standard output; behavior of such unhandled standard output depends on the future backend.

conditions

A character string of condition classes to be captured and relayed. The default is to relay all conditions, including messages and warnings. To drop all conditions, use conditions = character(0). Errors are always relayed. Attribute exclude can be used to ignore specific classes, e.g. conditions = structure("condition", exclude = "message") will capture all condition classes except those that inherit from the message class. Using conditions = structure(..., drop = TRUE) causes any captured conditions to be dropped from the future object as soon as they have been relayed, e.g. by value(f). This can help decrease the overall memory consumed by captured conditions across futures. Using conditions = NULL (not recommended) avoids intercepting conditions, except from errors; behavior of such unhandled conditions depends on the future backend and the environment from which R runs.

scheduling

Average number of futures ("chunks") per worker. If 0.0, then a single future is used to process all elements of X. If 1.0 or TRUE, then one future per worker is used. If 2.0, then each worker will process two futures (if there are enough elements in X). If Inf or FALSE, then one future per element of X is used. Only used if chunk_size is NULL.

chunk_size

The average number of elements per future ("chunk"). If Inf, then all elements are processed in a single future. If NULL, then argument scheduling is used.

...

Additional named options.

Value

A named list of future options. Attribute specified is a character vector of future options that were explicitly specified.

Examples

# Default futurize options
str(futurize_options())


List packages and functions supporting futurization

Description

List packages and functions supporting futurization

Usage

futurize_supported_packages()

futurize_supported_functions(package)

Arguments

package

A package name.

Value

A character vector of package or function names.

Examples

pkgs <- futurize_supported_packages()
pkgs

fcns <- futurize_supported_functions("base")
fcns


Transpile an R expression

Description

Transpile an R expression

Usage

transpile(
  expr,
  options = list(...),
  ...,
  when = TRUE,
  eval = TRUE,
  envir = parent.frame(),
  type = "built-in",
  what = "transpile",
  unwrap = list(base::`{`, base::`(`, base::`!`, base::local, base::I, base::identity,
    base::invisible, base::suppressMessages, base::suppressWarnings,
    base::suppressPackageStartupMessages),
  debug = FALSE
)

Arguments

expr

An R expression, typically a function call to transpile. If FALSE, then the transpiler is disabled, and if TRUE, it is re-enabled. If NA, then TRUE is returned if the transpiler is enabled, otherwise FALSE.

options

(optional) Named options for the transpilation.

when

If TRUE (default), the expression is transpiled, otherwise not.

eval

If TRUE (default), the transpiled expression is evaluated, otherwise it is returned.

envir

The environment where the expression should be evaluated.

type

Type of the transpiler to use.

unwrap

(optional) A list of functions that should be considered wrapping functions that the transpiler should unwrap ("enter"). This allows us to transpile expressions within { ... } and local( ... ).

Value

Returns the value of the evaluated expression expr if eval = TRUE, otherwise the transpiled expression. If expr is NA, then TRUE is returned if the transpiler is enabled, otherwise FALSE.


Options used by futurize

Description

Below are the R options and environment variables that are used by the futurize package and packages enhancing it.

WARNING: Note that the names and the default values of these options may change in future versions of the package. Please use with care until further notice.

Packages must not change future options

Just like for other R options, as a package developer you must not change any of the below ⁠futurize.*⁠ options. Only the end-user should set these. If you find yourself having to tweak one of the options, make sure to undo your changes immediately afterward.

Options for debugging

futurize.debug:

(logical) If TRUE, extensive debug messages are generated. (Default: FALSE)

Environment variables that set R options

All of the above R futurize.* options can be set by corresponding environment variable R_FUTURIZE_* when the futurize package is loaded. This means that those environment variables must be set before the futurize package is loaded in order to have an effect. For example, if R_FUTURIZE_DEBUG=true, then option futurize.debug is set to TRUE (logical).

See Also

To set R options or environment variables when R starts (even before the future package is loaded), see the Startup help page. The startup package provides a friendly mechanism for configuring R's startup process.

mirror server hosted at Truenetwork, Russian Federation.