在dplyr中使用udf进行标准评估 [英] Using standard evaluation with a udf in dplyr
问题描述
some_udf< - function(x)mean(x + 3)
generic_function< ; - function(dat,input_var){
dat%>%dplyr :: summarise_(mean_3 = sprintf(some_udf(%s),input_var))
}
运行泛型函数时,我会收到以下错误:
generic_function(mtcars,'cyl')
错误:找不到函数some_udf
当$ code> some_udf 被基本R函数替换时,像意味着
或 sd
,一切正常。
有人可以向我解释为什么udf不在这个案例和解决方案可能是什么?
这实际上是这个问题,它没有被接受的ans但是正如David Arenburg和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
I am programming with dplyr and therefore I am using standard evaluation. I make a generic function with a data frame and a column name as arguments. Within the function I would like to apply another function that I wrote myself on 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 running the generic function I'll get the following error:
generic_function(mtcars, 'cyl')
Error: could not find function "some_udf"
When some_udf
is replaced by a base R function, like mean
or sd
, all works fine.
Could someone explain to me why the udf is not working in this case and what a solution might be?
This is essentially a duplicate of this question, which doesn't have an accepted answer, but as pointed out in the comments by David Arenburg and MrFlick, you need to pass the expression as a formula so that the correct environment context is available:
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
这篇关于在dplyr中使用udf进行标准评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!