[R-sig-hpc] nested foreach loops

Stephen Weston stephen.b.weston at gmail.com
Fri Aug 19 20:18:26 CEST 2011


The main vignette for the foreach package contains two examples of
"when" in section 6, "List Comprehensions".  Use "vignette('foreach')"
to view it.  The foreach man page also shows one of the same examples,
the "qsort" example.  FInally, the "examples" directory in the
"foreach" installation directory contains three examples that use
"when": for.R, comprehensions.R, and qsort.R.  Those are all part of
the standard foreach package.

- Steve


On Fri, Aug 19, 2011 at 2:10 AM, Maas James Dr (MED) <J.Maas at uea.ac.uk> wrote:
> I'm using the following nested foreach loop structure which seems to work quite well.
>
> nsim = 1000 so the inner loop executes 1000 times.   I'd like to get the inner loop to randomly pick one result of the 1000 and graph it, but can't figure out how to create a function to do this.
>
> the line
>
> graphnum <- sample(1:nsim, 1)
>
> would randomly generate a variable between 1 and 1000 that I can then use to test and do the graphing when true.  My question is where can I put this ?? I've tried putting it between the two foreach statments and that doesn't work.
>
> Also, this structure is noted in the package definition for the foreach package ... but I can not find an example anywhere to show how to use them.  Would anyone have working examples?
>
> when (cond)
> e1 %:% e2
>
> Thanks
>
> Jim
>
> ======================
>
> mpiresults <- foreach (j=1:nrow(inputpars), .combine=rbind) %:%
>  foreach (i=1:nsim, .combine=rbind, .final=finalprocess) %dopar%
>
> ======================
>
>
> ===============================
> Dr. Jim Maas
> University of East Anglia
>
> _______________________________________________
> R-sig-hpc mailing list
> R-sig-hpc at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-hpc
>



More information about the R-sig-hpc mailing list