Skip to contents

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)
#> [easypar] 2022-05-30 14:21:09 - Overriding parallel execution setup [TRUE] with global option : FALSE
#> $`1`
#> [1] 1
#> 
#> $`2`
#> [1] 2
#> 
#> $`3`
#> [1] 3
#> 
#> $`4`
#> [1] 4
#> 
#> $`5`
#> [1] 5
#> 

# Run serially with progress_bar
run(dummy_fun, dummy_params, parallel = FALSE)
#> [easypar] 2022-05-30 14:21:09 - Overriding parallel execution setup [FALSE] with global option : FALSE
#> $`1`
#> [1] 1
#> 
#> $`2`
#> [1] 2
#> 
#> $`3`
#> [1] 3
#> 
#> $`4`
#> [1] 4
#> 
#> $`5`
#> [1] 5
#> 
# Run serially without progress_bar
run(dummy_fun, dummy_params, parallel = FALSE, progress_bar = FALSE)
#> [easypar] 2022-05-30 14:21:09 - Overriding parallel execution setup [FALSE] with global option : FALSE
#> $`1`
#> [1] 1
#> 
#> $`2`
#> [1] 2
#> 
#> $`3`
#> [1] 3
#> 
#> $`4`
#> [1] 4
#> 
#> $`5`
#> [1] 5
#> 

# Run parallel, not silent, caching the results
run(dummy_fun, dummy_params, silent = FALSE, cache = "MyCache.rds")
#> [easypar] 2022-05-30 14:21:09 - Overriding parallel execution setup [TRUE] with global option : FALSE
#> [easypar] 2022-05-30 14:21:09 - Computing 5 tasks.
#> [easypar] 2022-05-30 14:21:09 - Caching outputs to : /tmp/RtmpNAIm1Q/file339723f4d660/reference/MyCache.rds
#> [easypar] 2022-05-30 14:21:09 - Running sequentially.
#> $`1`
#> [1] 1
#> 
#> $`2`
#> [1] 2
#> 
#> $`3`
#> [1] 3
#> 
#> $`4`
#> [1] 4
#> 
#> $`5`
#> [1] 5
#> 
file.remove("MyCache.rds")
#> [1] TRUE

# Overwriting default setting with global options
options(easypar.parallel = FALSE)
run(dummy_fun, dummy_params) # Will run sequentially
#> [easypar] 2022-05-30 14:21:09 - Overriding parallel execution setup [TRUE] with global option : FALSE
#> $`1`
#> [1] 1
#> 
#> $`2`
#> [1] 2
#> 
#> $`3`
#> [1] 3
#> 
#> $`4`
#> [1] 4
#> 
#> $`5`
#> [1] 5
#> 

options(easypar.progress_bar = FALSE) # Will hide the progress bar
run(dummy_fun, dummy_params)
#> [easypar] 2022-05-30 14:21:09 - Overriding parallel execution setup [TRUE] with global option : FALSE
#> [easypar] 2022-05-30 14:21:09 - Overriding progress bar setup [TRUE] with global option : FALSE
#> $`1`
#> [1] 1
#> 
#> $`2`
#> [1] 2
#> 
#> $`3`
#> [1] 3
#> 
#> $`4`
#> [1] 4
#> 
#> $`5`
#> [1] 5
#> 

# 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
)
#> [easypar] 2022-05-30 14:21:09 - Overriding parallel execution setup [TRUE] with global option : TRUE
#> [easypar] 2022-05-30 14:21:09 - Overriding progress bar setup [TRUE] with global option : FALSE

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

numErrors(results)
#> [1] 5

filterErrors(results)
#> NULL

#' 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
)
#> [easypar] 2022-05-30 14:21:10 - Overriding parallel execution setup [TRUE] with global option : TRUE
#> [easypar] 2022-05-30 14:21:10 - Overriding progress bar setup [TRUE] with global option : FALSE
#> [easypar] 3/5 computations returned errors and will be removed.