r - foreach cannot find `i` used in foreach(i=1:N) -
i'm having troubles variables inside foreach. load cluster , set couple of vectors:
library(doparallel) ncores <- detectcores() - 2 cl <- makecluster(ncores, outfile="", port=11439) registerdoparallel(cl) results <- rep(na,10) values <- 20:30 then, not work:
# error: object 'i' not found foreach(i=1:10) %dopar% results[i] <- stopcluster(cl) while does:
# ok foreach(i=1:10) %dopar% values[i] stopcluster(cl) how come finds i when used inside [i] in left hand side, not find when used in right hand side?
from comment:
try curly braces.
foreach(i=1:10) %dopar% { results[i] <- } not example, experienced better use curly braces in r. many problems can avoided using them. , apparently there more advantages of these little helpers, may see while browsing through internets (e.g. see here).
Comments
Post a Comment