3

Following on from questions here and here, I'm trying to get the latest version of multidplyr to work with a custom function.

By way of reproducible example, I have tried:

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

But I get a Computation failed error. I have tried different ordering and a few other minor variations but no luck.

Is it possible to export custom functions to the clusters in the latest version of multidplyr? If so, how?

Will T-E
  • 607
  • 1
  • 7
  • 16

1 Answers1

0

Does the following achieve what you intended?

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

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

Mike Lawrence
  • 1,641
  • 5
  • 20
  • 40