[R] parallel computing with 'foreach'

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Jun 30 19:24:34 CEST 2011


Type
   ?foreach
and read the whole help page - as the positng guide asked you to do 
before posting, you will find the line describing the argument ".packages".

Uwe Ligges



On 28.06.2011 21:17, Stacey Wood wrote:
> Hi all,
> I would like to parallelize some R code and would like to use the 'foreach'
> package with a foreach loop.  However, whenever I call a function from an
> enabled package outside of MASS, I get an error message that a number of the
> functions aren't recognized (even though the functions should be defined).
> For example:
>
> library(foreach)
> library(doSMP)
> library(survival)
> # Create the simplest test data set
> test1<- list(time=c(4,3,1,1,2,2,3),
>                status=c(1,1,1,0,1,1,0),
>                x=c(0,2,1,1,1,0,0),
>                sex=c(0,0,0,0,1,1,1))
> # Fit a stratified model
> coxph(Surv(time, status) ~ x + strata(sex), test1)
>
> w<- startWorkers()
> registerDoSMP(w)
> foreach(i=1:3) %dopar% {
> # Fit a stratified model
> fit<-coxph(Surv(time, status) ~ x + strata(sex), test1)
> summary(fit)$coef[i]
> }
> stopWorkers(w)
> ####Error message:
> Error in { : task 1 failed - "could not find function "coxph""
>
>
> If I call library(survival) inside the foreach loop, everything runs
> properly.  I don't think that I should have to call the package iteratively
> inside the loop.  I would like to use a foreach loop inside code for my own
> package, but this is a problem since I can't call my own package in the
> source code for the package itself!  Any advice would be appreciated.
>
> Thanks,
> Stacey
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list