I have noticed a different behaviour using %do%
and %dopar%
concerning the management of the variables. Here is a MWE to show what I mean
library(foreach)
library(doParallel)
library(iterators)
a = 1
foreach(icount(10)) %do% {a = a + 1}
print(a)
# [1] 11
registerDoParallel(cores = 1)
a = 1
foreach(icount(10)) %dopar% {a = a + 1}
print(a)
# [1] 1
In the first case a
is incremented while in the second one the loop takes a copy and a
does not change. I was expected the second behaviour with %dopar%
to apply also with %do%
. Is there a way to enforce that without using %dopar%
(because I'm using nested foreach
loops as in the foreach
vignette).