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.