I have a tidyeval question that is puzzling me, which I'd be grateful for any help or thoughts.
Is it possible to use this grouped_mean function with character variable inputs?
library(tidyverse)
grouped_mean <- function(data, group_var, summary_var) {
group_var <- enquo(group_var)
summary_var <- enquo(summary_var)
data %>%
group_by(!!group_var) %>%
summarise(mean = mean(!!summary_var))
}
grouped_mean(mtcars, cyl, mpg) # this works
grouped_mean(mtcars, "cyl", "mpg") # but this doesn't work, and I would like it to
Ideally I want to not modify the grouped_mean function, but instead modify how the character variable input comes into the function
The reason I ask is that I have made some similar tidyeval functions for graphing and mapping that I would like to use shiny inputs
I'm hoping someone comes along with a recommended way for a function to take both bare symbols and strings as inputs since I haven't found anything that seems particularly straightforward.
One way, which seems pretty roundabout, is to convert everything (symbols or strings) to strings with rlang::as_name(). Then sym() can be used to convert the strings to symbols (as I said, roundabout ).
Then using either strings or symbols as inputs returns the same output.
grouped_mean(mtcars, "cyl", "mpg")
# A tibble: 3 x 2
# cyl mean
# <dbl> <dbl>
#1 4 26.7
#2 6 19.7
#3 8 15.1
> grouped_mean(mtcars, cyl, mpg)
# A tibble: 3 x 2
# cyl mean
# <dbl> <dbl>
#1 4 26.7
#2 6 19.7
#3 8 15.1
Edited to add another approach
I thought there should be some way to use a conditional statement within the function. This would be based on checking whether the input was a symbol or not and then use enquo() or sym() based on that. It looks like rlang::quo_is_symbol() is the predicate symbol that can do the checking.