1

If foreach calls a vectorized function fvec, which then calls a further function g, I get an error message saying that g can't be found.

This code

library(doParallel)
f <- function(x){
  x+g(x)
}
g <- function(x){
  x^2
}
fvec <- Vectorize(f)
registerDoParallel(cores=2)
foreach(i=1:5,.combine=rbind) %dopar% {
  fvec(i)
}

produces this error

Error in { : task 1 failed - "could not find function "g""

However, using f instead of fvec works, using %do% instead of %dopar% also works.

How do I get %dopar% to work properly in this instance. I'm using Windows.

LCE
  • 111
  • 1
  • Just tried it, it doesn't fix it. It's able to detect the `g` when using `f` instead of the vectorized version. Moving `fvec <- Vectorize(f)` inside the `foreach` seems to work though. – LCE Oct 16 '20 at 03:46

0 Answers0