0

What is wrong here? This works:

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

and produces:

    msl   msw
1 5.006 0.246

But this function doesn't work:

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

and means(iris, "setosa") produces this error:

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(...))

william3031
  • 1,653
  • 1
  • 18
  • 39
  • Why do you do `filter = enquo(value)`? You're not using that `filter` at all. The `filter` a few lines later will be the `filter` function. Or actually, it will be *a* filter function, it could be the one in `dplyr` or it could be the one `dplyr` stomps on or some other function. Make a minimal reproducible example with all the library calls and all the version numbers. – Spacedman Nov 17 '18 at 12:18
  • The version number of tidyverse doesnt show the version numbers of the packages you are actually using. List them. Which packages are you using? – Spacedman Nov 17 '18 at 12:19
  • This is a reproducible example. It is the `iris` dataset that comes with R. – william3031 Nov 17 '18 at 12:19
  • Just the tidyverse. This is just an example dataset. – william3031 Nov 17 '18 at 12:19
  • Changed the `value <- enquo(value)` bit. Still the same result. I had originally called it `filter`. – william3031 Nov 17 '18 at 12:21
  • Why are you enquo'ing the data? Where did you get the idea that that was the right thing to do?Also there is no "just the tidyverse". Your code runs with dplyr and rlang. – Spacedman Nov 17 '18 at 12:38
  • dplyr 0.7.7 rlang 0.2.2 - Idea from articles such as: https://sebastiansauer.github.io/prop_fav/ and https://dplyr.tidyverse.org/articles/programming.html – william3031 Nov 17 '18 at 12:42
  • By the way, I tried the other combinations of `quo`, not using `!!`, and using `UQ(value)` as well before I posted here. – william3031 Nov 17 '18 at 12:44
  • This can be done with using tidyeval: `means <- function(data, ...){ data %>% filter(Species == ..1) %>% summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width)) }` – G. Grothendieck Nov 17 '18 at 14:02
  • @wl1234: I suggest you read more about tidy evaluation https://tidyeval.tidyverse.org/getting-up-to-speed.html & https://maraaverick.rbind.io/2017/08/tidyeval-resource-roundup/ – Tung Nov 17 '18 at 18:07
  • Thanks for the info. – william3031 Nov 17 '18 at 20:34

1 Answers1

5

The error message is pretty straightforward, you can't filter a quosure. I don't know why you are enquo'ing your data, but this will get you what you want:

means <- function(data, value){

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

}
feebarscevicius
  • 584
  • 3
  • 7