The easypar R package allows you to:
run R functions in a parallel fashion in a trivial way. easily switch between parallel and serial executions of the same calls (at runtime). save results of paralle computation as far as they are produced (i.e., cache).
##Install
see: https://github.com/caravagn/easypar
devtools::install_github("caravagn/easypar")
## Skipping install of 'easypar' from a github remote, the SHA1 (e71e97bf) has not changed since last install.
## Use `force = TRUE` to force installation
function test, combined input text and output them.
merge_text = function(x){
paste0(x, collapse = "_")
}
test = function(i, label1, label2){
text = merge_text( c(i, label1, label2))
print(text)
}
label1 = "label1"
label2 = "lable2"
test(1, label1, label2)
## [1] "1_label1_lable2"
#"1_label1_lable2"
We store them in a list where each position is a full set of parameters that we want to pass to each calls to f (list of lists), named according to the actual parameter names.
inputs = lapply(1:10, function(x) list(i = x, label1 = label1, label2 = label2) )
library(easypar)
easypar provides a single function that takes as input f, its list of inputs and some execution parameters for the type of execution requested. The simplest call runs f in parallel, without seeing any output and just receiving the return values in a list as follows
export: export functions defined by Environment. for examples, function merge_text in the examples. packages: the import packages.
runs = easypar::run(FUN = test,
PARAMS = lapply(1:5, function(x) list(i = x, label1 = label1, label2 = label2) ) ,
parallel = TRUE,
outfile = NULL,
export = c("merge_text"),
packages = "tidyverse",
filter_errors = FALSE
)
runs
## $`1`
## [1] "1_label1_lable2"
##
## $`2`
## [1] "2_label1_lable2"
##
## $`3`
## [1] "3_label1_lable2"
##
## $`4`
## [1] "4_label1_lable2"
##
## $`5`
## [1] "5_label1_lable2"
numErrors(runs)
## [1] 0