enn*_*ppi 4 parallel-processing r snow dplyr magrittr
假设我想以并行方式应用myfunction到myDataFrame. 假设这otherDataFrame是一个包含两列的数据框:COLUNM1_odf并且COLUMN2_odf出于某些原因在myfunction. 所以我想用这样的方式编写代码parApply:
clus <- makeCluster(4)
clusterExport(clus, list("myfunction","%>%"))
myfunction <- function(fst, snd) {
#otherFunction and aGlobalDataFrame are defined in the global env
otherFunction(aGlobalDataFrame)
# some code to create otherDataFrame **INTERNALLY** to this function
otherDataFrame %>% filter(COLUMN1_odf==fst & COLUMN2_odf==snd)
return(otherDataFrame)
}
do.call(bind_rows,parApply(clus,myDataFrame,1,function(r) { myfunction(r[1],r[2]) }
Run Code Online (Sandbox Code Playgroud)
这里的问题是 R 无法识别COLUMN1_odf,COLUMN2_odf即使我将它们插入clusterExport. 我怎么解决这个问题?有没有办法“导出”所有snow需要的对象,以便不枚举它们中的每一个?
编辑1:我添加了一个注释(在上面的代码中),以指定的otherDataFrame被interally创建myfunction。
编辑 2:为了概括,我添加了一些伪代码myfunction:它现在使用全局数据帧(aGlobalDataFrame和另一个函数otherFunction)
做了一些实验,所以我解决了我的问题(在本杰明的建议下,并考虑了我添加到问题中的“编辑”):
clus <- makeCluster(4)
clusterEvalQ(clus, {library(dplyr); library(magrittr)})
clusterExport(clus, "myfunction", "otherfunction", aGlobalDataFrame)
myfunction <- function(fst, snd) {
#otherFunction and aGlobalDataFrame are defined in the global env
otherFunction(aGlobalDataFrame)
# some code to create otherDataFrame **INTERNALLY** to this function
otherDataFrame %>% dplyr::filter(COLUMN1_odf==fst & COLUMN2_odf==snd)
return(otherDataFrame)
}
do.call(bind_rows, parApply(clus, myDataFrame, 1,
{function(r) { myfunction(r[1], r[2]) } )
Run Code Online (Sandbox Code Playgroud)
这样一来,我注册的aGlobalDataFrame,myfunction并且otherfunction,总之所有的功能和函数使用的数据用于并行作业(myfunction本身)
| 归档时间: |
|
| 查看次数: |
1567 次 |
| 最近记录: |