3
votes

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
If all these functions live in your package namespace, you don't (need to) assume that the user has loaded anything into "the local environment".Roland

1 Answers

3
votes

I usually do this with the following code:

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