Parallelization of outer loop works in REvolution, but not in normal R

foreach, loops, nested, r

Solution

In general, if you need to use a package inside a `foreach` loop, you should specify it with the ".packages" option. That's even true for the `foreach` package itself:

library(doParallel)
cl <- makePSOCKcluster(3)
registerDoParallel(cl)
foreach(j = X, .combine = c, .packages='foreach') %dopar% { 
    Z=1
    foreach(i = Y, .combine = c) %do% { 
        paste(j, i, Z, sep = "") 
    } 
}

This makes some sense when you realize that the PSOCK cluster doesn't even know that it's being used by `foreach`. It's just a normal PSOCK cluster.

Note that it may not be necessary to specify all required packages with some parallel backends. For example, with doMC the workers inherit all currently loaded packages. However, it is good practice to specify them so that the code is more portable.

Problem

I am trying to parallelize an outer loop, while running the inner loop sequentially. The following code works in Revolution when using the `doSMP` package, but it does not work in base R when using the `foreach` and `doParallel` packages (both R versions on a Windows machine). The error message is: `could not find function "%do%"`. Any ideas how to resolve this issue? ``` foreach(j = X, .combine = c) %dopar% { Z=1 foreach(i = Y, .combine = c) %do% { paste(j, i, Z, sep = "") } } ```

Original source