Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export R package functions to parallel cluster within the R package

There are functions (like function1) within an R package I'm working on which rely on helper functions (like h_function1 and h_function2) in my package. I'm parallelizing to repeatedly call function1 within another function in my package.

Currently, in my package I'm using the code

parallel::clusterExport(cl, varlist=c("h_function1", "h_function2"), envir=environment())
parallel::parSapply(X=1:100, FUN=function1, cl=cl)

where cl is cluster from the parallel package that was passed into the function. This however assumes that the helper functions h_function1 and h_function2 are loaded from my package into the local environment, which may not be true for some users.

What is a way to flexibly export the functions to the cluster? Is it possible to export all functions of the package by package name?

like image 576
user257566 Avatar asked Oct 27 '25 05:10

user257566


1 Answers

I usually do this with the following code:

parallel::clusterEvalQ({library("<package-name>")})
like image 54
snaut Avatar answered Oct 29 '25 21:10

snaut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!