You’re getting the error when calling
clusterExport(cl, list("a", "b", "data")) because
clusterExport is trying to find the variables in
fn1 isn’t setting them in
.GlobalEnv but in its own local environment.
An alternative is to pass the local environment of
fn2, and specify that environment to
clusterExport. The call to
fn2 would be:
c <- fn2(m, environment())
If the arguments to
function(x, env), then the call to
clusterExport would be:
clusterExport(cl, list("a", "b", "data"), envir = env)
Since environments are passed by reference, there should be no performance problem doing this.
Read more here: Source link