我正在尝试在脚本中用parSapply替换sapply,以减少运行时间。
这是我在数据框示例中使用的脚本:
library(relaimpo)
gene1 <- c(1, 2, 3, 4)
gene2 <- c(3,1,2, 4)
age <- c(20, 40, 60, 10)
gender <- c("F", "M", "F", "F")
datatest<- data.frame(gene1, gene2, age, gender)
TEST<-sapply(datatest[,(1:2)], function(i) calc.relimp(lm(i ~ age+gender, data=datatest))$lmg)
##until now it works; Here is when I replace sapply by parSapply:
cl <- makeCluster(4)
TEST2 <- parSapply(cl, datatest[,(1:2)], function(i) calc.relimp(lm(i ~ age+gender, data=datatest))$lmg)
我收到此错误消息:
checkForRemoteErrors(val)中的错误:2个节点产生了错误;第一 错误:找不到函数“ calc.relimp”
有人知道如何解决此问题吗? 提前非常感谢!!!
最佳,
Bérengère
答案 0 :(得分:1)
您需要在集群中导出所需的对象。像
clusterExport(cl, varlist = c('calc.relimp')) # add other objects needed
应该可以解决问题。