Re: [R] dopar parallel assignments

From: David M Smith <>
Date: Thu, 29 Apr 2010 15:44:34 -0700

[This relates to the foreach function in library(foreach)]


In the %dopar% example, the assignments are being made into "child" R sessions in parallel. foreach is generally pretty good about detecting variables you reference in the parallel loops and making sure the objects are copied over to the parent, but in this case where you're using "assign" directly, it doesn't detect that test_1.25 and tets_1.50 are variables you're modifying in parallel. (In the %do% case everything is run in the same R session, so the assignments are naturally preserved.)

Also, in general, think of foreach more of an analogue to "lapply" than to "for", in the sense that you're running it to collect the values of the body of the loop, not for their side effects.

So what you probably really want is code like this:

test <- foreach(i = c(1.25, 1.50)) %dopar% some_timeconsuming_operation(i)

and then (if you really need the variables named as specified)

v <- c(1,25,1.50)
for (i in seq(along=v)) assign(paste("test_",v[i],sep=""),test[[i]])

In your specific example,

 some_timeconsuming_function <- function(i) i

but that's not timeconsuming, and so you're not going to get any benefit from parallelization.

# David Smith

On Thu, Apr 29, 2010 at 2:07 PM, Vivek Ayer <> wrote:
> Hi guys,
> I was wondering why this piece of code doesn't work:
> foreach (i = c(1.25,1.50)) %dopar% {
> assign(paste("test_",i,sep=""),i)
> }
> but, this does:
> foreach (i = c(1.25,1.50)) %do% {
> assign(paste("test_",i,sep=""),i)
> }
> Obviously, the difference is %dopar% vs. %do%. If I use %do%, I get
> objects test_1.25 and test_1.50, but I don't get these objects if I
> use %dopar% even though it seemed to run through the loop in parallel.
> Thanks in advance,
> Vivek
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.

David M Smith <>
VP of Marketing, REvolution Computing
Tel: +1 (650) 330-0553 x205 (Palo Alto, CA, USA)

Download REvolution R free:

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 29 Apr 2010 - 22:47:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 30 Apr 2010 - 01:00:16 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive