3

继问题herehere之后,我正在尝试获取最新版本multidplyr以使用自定义函数。

通过可重现的示例,我尝试过:

library(multidplyr)
library(dplyr)
cl <- new_cluster(3)
df <- data.frame(Grp = rep(LETTERS[1:3], each = 4), Val = rep(3:1, 4))

cust_func <- function (x) {
  x + 1
}

cluster_copy(cl, "cust_func")

df_clust <- df %>%
  group_by(Grp) %>%
  partition(cl) 

df_clust %>%
  mutate(Add1 = cust_func(Val)) %>%
  collect()

但我得到一个Computation failed错误。我尝试了不同的排序和其他一些小的变化,但没有运气。

是否可以将自定义函数导出到最新版本的集群中multidplyr?如果是这样,怎么做?

4

1 回答 1

0

以下是否达到您的预期?

new_cust_func <- function (x) {
  x$Val + 1
  return(x)
}

df_clust %>%
  do(new_cust_func(.)) %>%
  collect()

于 2020-12-21T14:14:05.893 回答