Reputation: 137
I'm trying to understand tidyeval and came across a tutorial from Lionel Henry. I've also read the vignette from dplyr. They describe different methods of passing multiple arguments, e.g. when using group_by
.
What's the difference between the two examples below? In simple terms(if possible): Which one should I use and why?
library(dplyr)
mean_by <- function(data, var, ...) {
var <- enquo(var)
data %>%
group_by(...) %>%
summarise(avg = mean(!! var))
}
mean_by2 <- function(data, var, ...) {
var <- enquo(var)
group <- quos(...)
data %>%
group_by(!!! group) %>%
summarise(avg = mean(!! var))
}
all_equal(mean_by(starwars, height, species, eye_color),
mean_by2(starwars, height, species, eye_color))
#> [1] TRUE
Created on 2018-10-05 by the [reprex
package](http://reprex.tidyverse.org) (v0.2.0).
Upvotes: 1
Views: 75
Reputation: 6803
They are functionally equivalent. If you're not modifying the arguments in the dots (changing their names, wrapping a function around the arguments, ...), it's better to pass the ...
directly as it's more concise and readable.
This section of the tidy eval bookdown explores these issues: https://tidyeval.tidyverse.org/multiple.html
Upvotes: 2