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?
Edwin source
share