I would like to do some parallel computing in R on Windows.
library(doSNOW)
library(foreach)
cl<-makeCluster(4)
registerDoSNOW(cl)
nn = seq(2,20, by=2)
nn2 = rep(-1,10)
vector.accessed.within.iteration = rep(0, 10)
y = foreach(i=1:10) %dopar% {
print(paste0('i = ', i))
nn2[i] = (nn[i])^2
vector.accessed.within.iteration[i] = nn[i] + nn2[i]
nn2[i]
}
stopCluster(cl)
When running the code, y
is returned as the list of squares from 2 to 20, as intended. However, neither vector nn2
nor vector.accessed.within.iteration
are modified within foreach
loop. Furthermore, the printing print(paste0('i = ', i))
does not occur.
Is it possible to run iterations of a loop in parallel on windows while also displaying messages and modifying variables from global environment?