furrr (version 0.1.0)

future_map: Apply a function to each element of a vector via futures

Description

These functions work exactly the same as purrr::map() functions, but allow you to run the map in parallel. The documentation is adapted from both purrr::map(), and future.apply::future_lapply(), so look there for more details.

Usage

future_map(.x, .f, ..., .progress = FALSE, .options = future_options())

future_map_chr(.x, .f, ..., .progress = FALSE, .options = future_options())

future_map_dbl(.x, .f, ..., .progress = FALSE, .options = future_options())

future_map_int(.x, .f, ..., .progress = FALSE, .options = future_options())

future_map_lgl(.x, .f, ..., .progress = FALSE, .options = future_options())

future_map_dfr(.x, .f, ..., .id = NULL, .progress = FALSE, .options = future_options())

future_map_dfc(.x, .f, ..., .progress = FALSE, .options = future_options())

future_map_if(.x, .p, .f, ..., .progress = FALSE, .options = future_options())

future_map_at(.x, .at, .f, ..., .progress = FALSE, .options = future_options())

Arguments

.x

A list or atomic vector.

.f

A function, formula, or atomic vector.

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

This syntax allows you to create very compact anonymous functions.

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. Within a list, wrap strings in get-attr() to extract named attributes. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to .f.

.progress

A logical, for whether or not to print a progress bar for multiprocess, multisession, and multicore plans.

.options

The future specific options to use with the workers. This must be the result from a call to future_options().

.id

If not NULL a variable with this name will be created giving either the name or the index of the data frame.

.p

A single predicate function, a formula describing such a predicate function, or a logical vector of the same length as .x. Alternatively, if the elements of .x are themselves lists of objects, a string indicating the name of a logical element in the inner lists. Only those elements where .p evaluates to TRUE will be modified.

.at

A character vector of names or a numeric vector of positions. Only those elements corresponding to .at will be modified.

Value

All functions return a vector the same length as .x.

future_map() returns a list, future_map_lgl() a logical vector, future_map_int() an integer vector, future_map_dbl() a double vector, and future_map_chr() a character vector. The output of .f will be automatically typed upwards, e.g. logical -> integer -> double -> character.

Examples

Run this code
# NOT RUN {
library(furrr)
library(dplyr) # for the pipe

# }
# NOT RUN {
plan(multiprocess)
# }
# NOT RUN {
1:10 %>%
  future_map(rnorm, n = 10) %>%
  future_map_dbl(mean)

# If each element of the output is a data frame, use
# future_map_dfr to row-bind them together:
mtcars %>%
  split(.$cyl) %>%
  future_map(~ lm(mpg ~ wt, data = .x)) %>%
  future_map_dfr(~ as.data.frame(t(as.matrix(coef(.)))))

# You can be explicit about what gets exported to the workers

# To see this, use multisession (NOT multicore if on a Mac as the forked workers
# still have access to this environment)
# }
# NOT RUN {
plan(multisession)
# }
# NOT RUN {
x <- 1
y <- 2

# This will fail, y is not exported (no black magic occurs)
# future_map(1, ~y, .options = future_options(globals = "x"))

# y is exported
future_map(1, ~y, .options = future_options(globals = "y"))


# }

Run the code above in your browser using DataCamp Workspace