rdoparallelparallel-foreach

StopIteration error in nested foreach/for loop


I've got a nasty error which only shows up when you ask R with geterrmessage() to actually display it. It also is no problem as long as you run the code in an interactive console session but when you call R non-interactively (in this case through a 3rd party software) this error results in the script being aborted.

Does anyone have an idea why the below code generates a (hidden) StopIteration error?

# Load the required packages
library(foreach)
library(parallel)
library(doParallel)

# ----- set-up cluster for parallel processing -----
ncl <- max(2,floor(detectCores()*0.75)) #number of cores
clst <- makePSOCKcluster(n=ncl) #create cluster for R
registerDoParallel(cl = clst) #register cluster

# ----- display latest error  -----
print(geterrmessage())

# ----- run nested foreach/for loop and stop cluster afterwards -----
t <- foreach (j=1:8) %dopar% {
        for (i in j:8) {
            print(paste("j",j,"i",i))
        }
    }
stopCluster(clst)

# ----- display latest error  -----
print(geterrmessage())

Thanks, Mark


Solution

  • try this black magic : .Internal(seterrmessage("")) idea sourced from stat.ethz.ch/pipermail/r-help/2006-February/087931.html