mappp: a wrapper around purrr::map()

Description Usage Arguments Details Examples

View source: R/mappp.R

Description

This function is a wrapper around purrr::map() with some extras on top, including parallel computation, progress bar, error handling, and result caching.

Usage

1
2
mappp(X, FUN., parallel = FALSE, cache = FALSE, cache.name = "cache",
  error.value = NA, quiet = TRUE, num.cores = NULL)

Arguments

X

List of objects to apply over

FUN.

Function to apply; allows for compact anonymous functions (see purrr::as_mapper() for details)

parallel

logical; use parallel processing?

cache

defaults to FALSE, which means no cache used. If TRUE, cache the results locally in a folder named according to cache.name using the memoise package

cache.name

a character string to use a custom cache folder name (e.g. "my_cache"); defaults to "cache"

error.value

(defaults to NA) use purrr::possibly to replace errors with this value instead of interrupting the process; set to NULL to not use error handling and instead interrupt the calculation

quiet

logical, suppress error messages until the end of calculation? or show them as they occur

num.cores

The number of cores used for parallel processing. Can be specified as an integer, or it will guess the number of cores available with detectCores(). If parallel is FALSE, the input here will be set to 1.

Details

mappp is designed for long computations and as such it always uses a progress bar, and always returns a list. Long computations shouldn't worry about being type strict; instead, extract results in the right type from the results list.

A progress bar will be shown in the terminal using an interactive R session or in an .Rout file, if using R CMD BATCH and submitting R scripts for non-interactive completion. Although R Studio supports the progress bar for single process workers, it has a problem showing the progress bar if using parallel processing (see the discussion at http://stackoverflow.com/questions/27314011/mcfork-in-rstudio). In this specific case (R Studio + parallel processing), text updates will be printed to the file '.process'. Use a shell and 'tail -f .progress' to see the updates.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
## Not run: 
X <- list('x' = 100, 'y' = 'a', 'z' = 200)
slow_log <- function(.x) {Sys.sleep(0.5); log(.x)}
# by default returns NA on error
mappp(X, slow_log)
# when not using error, entire calculation will fail
mappp(X, slow_log, error.value=NULL)
# showing error messages when they occur rather than afterwards can be useful
# but will cause problems with error bar displays
mappp(X, slow_log, quiet=FALSE)

## End(Not run)

cole-brokamp/CB documentation built on May 13, 2019, 8:49 p.m.