Error when using %dopar% instead of %do% in R (pac

2019-02-18 08:57发布

I've come up with a strange error. Suppose I have 10 xts objects in a list called data. I now search for every three combinations using

   data_names <- names(data)
   combs <- combn(data_names, 3)

My basic goal is to do a PCA on those 1080 triples. To speed things up I wanted do use the package doParallel. So here is the snippet shortened till the point where the error occurs:

list <- foreach(i=1:ncol(combs)) %dopar% {
    tmp_triple <- combs[,i]

    p1<-data[tmp_triple[[1]]][[1]]
    p2<-data[tmp_triple[[2]]][[1]]
    p3<-data[tmp_triple[[3]]][[1]]

    data.merge <- merge(p1,p2,p3,all=FALSE)
}

Here, the merge function seems to be the problem. The error is

task 1 failed - "cannot coerce class 'c("xts", "zoo")' into a data.frame"

However, when changing %dopar% to a normal serial %do% everything works as accepted.

Till now I was not able to find any solution to this problem and I'm not even sure what to look for.

3条回答
smile是对你的礼貌
2楼-- · 2019-02-18 09:12

The problem is likely that you haven't called library(xts) on each of the workers. You don't say what backend you're using, so I can't be 100% sure.

If that's the problem, then this code will fix it:

list <- foreach(i=1:ncol(combs)) %dopar% {
    library(xts)
    tmp_triple <- combs[,i]

    p1<-data[tmp_triple[[1]]][[1]]
    p2<-data[tmp_triple[[2]]][[1]]
    p3<-data[tmp_triple[[3]]][[1]]

    data.merge <- merge(p1,p2,p3,all=FALSE)
}
查看更多
闹够了就滚
3楼-- · 2019-02-18 09:18

A better solution rather than explicitly loading the libraries within the function would be to utilise the .packages argument of the foreach() function:

list <- foreach(i=1:ncol(combs),.packages=c("xts","zoo")) %dopar% {
    tmp_triple <- combs[,i]

    p1<-data[tmp_triple[[1]]][[1]]
    p2<-data[tmp_triple[[2]]][[1]]
    p3<-data[tmp_triple[[3]]][[1]]

    data.merge <- merge(p1,p2,p3,all=FALSE)
}
查看更多
Evening l夕情丶
4楼-- · 2019-02-18 09:20

Quick fix for problem with foreach %dopar% is to reinstall these packages:

install.packages("doSNOW")

install.packages("doParallel") 

install.packages("doMPI")

These are responsible for parallelism in R. Bug which existed in old versions of these packages is now removed. It worked in my case.

查看更多
登录 后发表回答