We set up a partial function like so:
x <- 3
func <- purrr::partial(rnorm, n = x, ... = , sd = 1)
func(5)
If we try to put it directly into a parallel function, it fails since it can't find x
(the argument for n
).
library(parallel)
cl <- makeCluster(detectCores())
parLapply(cl, 1:100, func)
So the solution I use is to use parallel::clusterExport(cl, "x")
. This is good, but I guess it could be a problem for more complex functions where x
is large. Is there a way to make the partial function self-contained? Or is there some better way to use partial functions inside a parallel algorithm?
Thanks!!!