run: Run a function with easypar

View source: R/run.R

runR Documentation

Run a function with easypar

Description

Run a task on a list of inputs, making easy to parallelize it or run it in a sequential session. If the glonal option easypar.parallel is set, the parameter parallel is overwritten. This allows to easily switch between debug and non-debug execution modes for this task.

Usage

run(
  FUN,
  PARAMS,
  packages = NULL,
  export = NULL,
  cores.ratio = 0.8,
  parallel = TRUE,
  silent = TRUE,
  outfile = "",
  cache = NULL,
  progress_bar = TRUE,
  filter_errors = TRUE
)

Arguments

FUN

R function to run

PARAMS

List of parameters, each entry needs to match the arguments of FUN

packages

Packages to load in each parallel thread

export

Variables to export to each parallel thread

cores.ratio

Ratio of the available cores that are used for a parallel run, default 80%.

parallel

Boolean value, TRUE for a parallel execution, and FALSE for a standard for loop.

silent

Silent output from easypar.

outfile

Output file for the parallel thread, default "" is console, NA is the default.

cache

Cache is used during computation to dump results to a template RDS file.

progress_bar

Boolean value, default TRUE. Print a progress_bar during the execution.

filter_errors

If 'TRUE', errors intercepted are filtered before returning results.

Examples


# A very simple function
dummy_fun = function(x) {x}

# Parameters
dummy_params = lapply(1:5, list)

# Default run: parallel, silent, output to screen
run(dummy_fun, dummy_params)

# Run serially with progress_bar
run(dummy_fun, dummy_params, parallel = FALSE)
# Run serially without progress_bar
run(dummy_fun, dummy_params, parallel = FALSE, progress_bar = FALSE)

# Run parallel, not silent, caching the results
run(dummy_fun, dummy_params, silent = FALSE, cache = "MyCache.rds")
file.remove("MyCache.rds")

# Overwriting default setting with global options
options(easypar.parallel = FALSE)
run(dummy_fun, dummy_params) # Will run sequentially

options(easypar.progress_bar = FALSE) # Will hide the progress bar
run(dummy_fun, dummy_params)

# Errors can be intercepted. Consider a function
# that can generate some error. The run will not plot and
# the computation will run anyway.

options(easypar.parallel = TRUE)

results = run(
  FUN = function(x) {
    if(runif(1) > .5) stop("Some error")
    x
  },
  PARAMS = dummy_params,
  silent = TRUE,
  filter_errors = FALSE
)

# Getters that can return the number of errors
# and filter them out

numErrors(results)

filterErrors(results)

#' Can do this automatically with filter_errors = TRUE
results = run(
  FUN = function(x) {
    if(runif(1) > .5) stop("Some error")
    x
  },
  PARAMS = dummy_params,
  silent = TRUE,
  filter_errors = TRUE
)

caravagn/easypar documentation built on June 4, 2022, 4:25 a.m.