Skip to content

Foreach (doMC)

2 messages · Rui Esteves, Jannis

#
Rui,

I suggest you read the following tutorial to give you an introduction to foreach:

http://cran.r-project.org/web/packages/doMC/vignettes/gettingstartedMC.pdf

Regarding your question I could would suspect that you can not assign values to some variable inside foreach and use them "outside" in the global environment. It is analog to using 'loacal' variables inside ordinary dfunctions.

Try running:

zappa <- foreach (i = 1:4) %dopar% {
  kmeans(iris[-5],4)$cluster
 }

You need to define the .combine function to combine the results from your calculation. I would suggest you try to restrict yourself to saving single vectors from each foreach loop for the sake of simplicity. I myself tried to save different types of R objects from foreach iterations once and it was a hard fight against the code....


HTH
Jannis


P.S. Is there any particular reason why there are so seldom answers to posts regarding foreach and all these doMC/doSMP packages ? Do so few people use these packages or does this have anything to do with the commercial origin of these packages? 


--- Rui Esteves <ruimaximo at gmail.com> schrieb am Mo, 17.10.2011: