Using standard udf evaluation in dplyr

I am programming dplyr, so I am using a standard grade. I am making a generic function with a data frame and a column name as arguments. Inside the function, I would like to apply another function that I wrote myself in the column of the data frame. Here is a minimal example:

some_udf <- function(x) mean(x + 3)
generic_function <- function(dat, input_var){
  dat %>% dplyr::summarise_(mean_3 = sprintf("some_udf(%s)", input_var))
}

Now when I run the generic function, I get the following error:

generic_function(mtcars, 'cyl')

Error: could not find function "some_udf"

When some_udfreplaced by a basic R function, such as meanor sd, everything works fine.

Can someone explain to me why udf does not work in this case and what solution could be?

+2
source share
2 answers

, , , , , MrFlick, , :

library(dplyr)

some_udf <- function(x) mean(x + 3)
generic_function <- function(dat, input_var){
    dat %>% 
        summarise_(mean_3 = as.formula(sprintf("~some_udf(%s)", input_var)))
}

generic_function(mtcars, 'cyl')
#   mean_3
# 1 9.1875
+3

nse interp:

generic_function <- function(dat, input_var){
  dat %>% 
    dplyr::summarise_(mean_3 = lazyeval::interp(~some_udf(x), x = as.name(input_var)))
}

generic_function(mtcars, 'cyl')
  mean_3
1 9.1875
+2

Source: https://habr.com/ru/post/1570059/


All Articles