I am building a function that requires the user to occasionally pass multiple variables into an argument. The function already uses ... for something else.
I was trying to do this using {{ }} but passing multiple variables to {{ }} only works sometimes.
Here are two functions that both use {{ }}. Both functions work when the user passes a single variable to {{ }}, but the second function fails when passed multiple variables.
Can you help me:
- Understand why the first function succeeds, and the second fails when given multiple variables?
- fix function 2 so I can pass multiple arguments without using ...
library(tidyverse)
# Function 1
select_and_summarise <- function(.data, selected, ...){
.data %>%
select({{selected}}) %>%
summarise_all(list(...))
}
# Function 2
group_and_summarise <- function(.data, group, ...){
.data %>%
group_by({{group}}) %>%
summarise_all(list(...))
}
# Passing single variables work
mtcars %>%
select_and_summarise(selected = cyl, mean = mean)
mtcars %>%
group_and_summarise(group = cyl, mean = mean)
# Passing more than one variable only works for select_and_summarise
mtcars %>%
select_and_summarise(selected = c(cyl, mpg, hp), mean = mean)
mtcars %>%
group_and_summarise(group = c(cyl, mpg, hp), mean = mean)
EDIT
Following Hadleys advice I made this function that works for both the examples I've given:
group_and_summarise <- function(.data, group, ...){
.data %>%
group_by_at(vars({{group}})) %>%
summarise_all(list(...))
}