Skip to content
Prev 624 / 2152 Next

foreach: using either %do% or %dopar% depending on condition

Hi

I wish to be able to substitute %do% or %dopar% depending on whether the 
user wants or not to use the parallel feature. For this, I would do 
something like:

if(hpc) foreach(icount(5), .combine = "rbind")  %dopar%
else foreach(icount(5), .combine = "rbind")  %do%
    statement of function...

I did not succeed in doing it, I tried:

foreach(icount(5), .combine = "rbind")  %do% else  %dopar% 
{
a<-runif(1)
b<-runif(1)
c(a,b)
}

Or tried to assign outside the operator:

"%my%" <-function(obj,ex) if(hpc=="none") "%dopar%" else "%dopar%"

foreach(icount(5), .combine = "rbind")  %my% {
a<-runif(1)
b<-runif(1)
c(a,b)
}

But it fails... any idea?

Thanks a lot!!

Matthieu