(见下面的工作解决方案)
我想使用 multidplyr 来并行化一个函数:
calculs.R
f <- function(x){
return(x+1)
}
main.R
library(dplyr)
library(multidplyr)
source("calculs.R")
d <- data.frame(a=1:1000,b=sample(1:2,1000),replace=T)
result <- d %>%
partition(b) %>%
do(f(.)) %>%
collect()
然后我得到:
Initialising 3 core cluster.
Error in checkForRemoteErrors(lapply(cl, recvResult)) :
2 nodes produced errors; first error: could not find function "f"
In addition: Warning message:
group_indices_.grouped_df ignores extra arguments
如何将源函数分配给每个内核?
==================
这是完美的脚本:
必须提取要更新的值,并将结果转换为数据帧
calcul.R
f <- function(x){
return(data.frame(x$a+1))
}
必须设置集群并分配源函数
main.R
library(dplyr)
library(multidplyr)
source("calculs.R")
cl <- create_cluster(3)
set_default_cluster(cl)
cluster_copy(cl, f)
d <- data.frame(a=1:10,b=c(rep(1,5),rep(2,5)))
result <- d %>%
partition(b) %>%
do(f(.)) %>%
collect()
最佳答案
看起来您初始化了一个集群(尽管您没有显示这部分)。您需要将全局环境中的变量/函数导出到每个工作人员。假设您将集群设为
cl <- create_cluster(3)
set_default_cluster(cl)
你能试一下吗
cluster_copy(cl, f)
这会将
f
复制并导出到每个 worker (我认为......)额外的
您可能会遇到另一个问题,即您的函数接受
x
作为参数,您向其添加 1f <- function(x){
return(x+1)
}
由于您将数据帧传递给
f
,因此您要求的是 data.frame+1
,这是没有意义的。您可能想将您的功能更改为类似f <- function(x){
return(x$a+1)
}
关于r - multidplyr : assign functions to cluster,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46553704/