Export R package functions to parallel cluster within the R package

542 views Asked by At

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?

1

There are 1 answers

0
snaut On BEST ANSWER

I usually do this with the following code:

parallel::clusterEvalQ({library("<package-name>")})