问题描述:

I am using doSMP as a parallel backend in Windows 7, with R 2.12.2. I incur in an error, and would like to understand the likely cause. Here is some sample code to reproduce the error.

require(foreach)

require(doSMP)

require(data.table)

wrk <- startWorkers(workerCount = 2)

registerDoSMP(wrk)

DF = data.table(x=c("b","b","b","a","a"),v=rnorm(5))

setkey(DF,x)

foreach( i=1:2) %dopar% {

DF[J("a"),]

}

The error message is

Error in { : task 1 failed - "could not find function "J""

网友答案:

I've not used doSMP, but I did some digging around and it looks like this post gets at a similar issue.

so it looks like you should be able to do:

foreach( i=1:2, .packages="data.table")  %dopar% {
    DF[J("a"),]
}

I can't test as I don't have a Windows machine handy.

网友答案:

OK, I asked Revolution computing, and Steve Weller (of RC) replied:

The problem is a R scoping issue. By default, foreach() will look for variables defined in it's own 'environment'. Any objects defined outside of it's scope need to be explicitly passed to it via the '.export' argument.

In your case, you will need to modify your 'foreach()' call to pass in the objects 'DF' and 'J':

...

foreach(i=1:2, .export=c("DF","J")) %dopar% {
...

I haven't tried either solution yet, but I trust both JD and RC...

相关阅读:
Top