Error when using %dopar% instead of %do% in R (package doParallel)

风格不统一 提交于 2019-12-01 06:44:50

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)
}

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)
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!