I'm trying to use foreach to do multicore computing in R.
A <-function(....) {
foreach(i=1:10) %dopar% {
B()
}
}
then I call function A
in the console. The problem is I'm calling a function Posdef
inside B
that is defined in another script file which I source. I had to put Posdef
in the list of export argument of foreach
: .export=c("Posdef")
. However I get the following error:
Error in { : task 3 failed - "could not find function "Posdef""
Why cant R find this defined function?
Quick fix for problem with foreach %dopar% is to reinstall these packages:
It worked in my case.
So I can reproduce this, for the curious:
I have been able to get around this by putting the function in another file and loading that file in the body of the foreach. You could also obviously move the function definition into the body of the foreach itself.
[EDIT -- I had previously suggested that perhaps .export doesn't work properly with function names, but was corrected below.]
The short answer is that this was a bug in parallel backends such as
doSNOW
,doParallel
anddoMPI
, but it has since been fixed.The slightly longer answer is that
foreach
exports functions to the workers using a special "export" environment, not the global environment. That used to cause problems for functions that were created in the global environment, because the "export" environment wasn't in their scope, even though they were now defined in that same "export" environment. Thus, they couldn't see any other functions or variables defined in the "export" environment, such as "Posdef" in your case.The
doSNOW
,doParallel
anddoMPI
backends now change the associated environment from the global to the "export" environment for functions exported via ".export", and seems to have resolved these issues.