基于sparklyr和标准评估(SE)的功能

时间:2018-02-12 17:41:41

标签: apache-spark dplyr sparklyr non-standard-evaluation standard-evaluation

我尝试编写一个执行的函数,sdf_pivot() a创建一个Spark DataFrame,其列名包含原始变量或列的名称。

set.seed(80)
df <- data.frame(id = c(1:5),
                 var1 = sample(LETTERS[1:12], 5, replace = TRUE),
                 var2 = sample(LETTERS[13:16], 5, replace = TRUE))

ref <- copy_to(sc, df, "mytbl")
glimpse(ref)

Observations: 5
Variables: 3
$ id   <int> 1, 2, 3, 4, 5
$ var1 <chr> "F", "G", "J", "A", "H"
$ var2 <chr> "M", "O", "O", "O", "O"

var1的预期结果,无需编写函数:

ref %>% 
  dplyr::select(id, var1) %>%
  dplyr::mutate(newvar1 = paste0("var1_",var1)) %>%
  sparklyr::sdf_pivot(formula = id ~ newvar1, fun.aggregate = "count") %>% 
  sparklyr::na.replace(0)

# Source:   table<sparklyr_tmp_56f96ab7d507> [?? x 6]
# Database: spark_connection
     id var1_A var1_F var1_G var1_H var1_J
  <int>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1     1      0      1      0      0      0
2     3      0      0      0      0      1
3     5      0      0      0      1      0
4     4      1      0      0      0      0
5     2      0      0      1      0      0

在我的一个功能版本中,当然不起作用,我也尝试使用quotedeparse,但我仍坚持使用mutate_sdf_pivot {1}}和myPivotFunction <- function(sdf, varname, newvarname){ mutate_op <- paste0(newvarname," = ", "var1_", varname) sdf %>% dplyr::select_(.dots = list('id', varname)) %>% mutate_(.dots = setNames(newvarname, mutate_op)) %>% sparklyr::sdf_pivot(formula = id ~ newvar1, fun.aggregate = "count") %>% sparklyr::na.replace(0) }

ProjectName.Droid.image_name.jpeg

1 个答案:

答案 0 :(得分:0)

一点library(rlang) library(glue) myPivotFunction <- function(sdf, varname, newvarname){ exprs <- c("id", glue('paste0("var1_", {varname})')) %>% setNames(c("id", newvarname)) %>% lapply(parse_quosure) sdf %>% transmute(!!! exprs) %>% sdf_pivot( formula = as.formula(glue("id ~ {newvarname}")), fun.aggregate = "count") %>% na.replace(0) } 应该可以解决问题:

public Field returnSelectedField() {
    FieldSelector fs = new FieldSelector(); //FieldSelector is the class I mentioned before
    return fs.saved;
}