4

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
  • 3
    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". Commented Oct 29, 2020 at 13:34

1 Answer 1

5
+25

I usually do this with the following code:

parallel::clusterEvalQ({library("<package-name>")})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.