功能问题。潮汐过滤

Function issue. Tidyeval filtering

这里有什么问题?这有效:

iris %>% 
  filter(Species == "setosa") %>% 
  summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width))

并产生:

    msl   msw
1 5.006 0.246

但是这个功能不起作用:

means <- function(data, value){
  data <- enquo(data)
  value <- enquo(value)
  data %>% 
    filter(Species == !!value) %>% 
    summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width))
}

means(iris, "setosa") 产生此错误:

Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "c('quosure', 'formula')" Called from: filter_(.data, .dots = compat_as_lazy_dots(...))

错误信息很简单,你不能过滤一个quosure。我不知道你为什么要 enquo'ing 你的数据,但这会让你得到你想要的:

means <- function(data, value){

  value <- enquo(value)
  data %>% 
    filter(Species == !!value) %>% 
    summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width))

}