I'm trying to get my head around using quasiquotation from the tidyverse in R in my own functions. I've read this one here: Passing a list of arguments to a function with quasiquotation and the whole thing here: https://tidyeval.tidyverse.org/
But I still don't get it to work.
Assume I have the following data:
dat <- data.frame(time = runif(20),
group1 = rep(1:2, times = 10),
group2 = rep(1:2, each = 10),
group3 = rep(3:4, each = 10))
What I want to do now is to write a function that does the following:
- take a data set
- specify the variable that contains the time (note, in another data set this might be called "hours" or "qtime" or whatever)
- specify by which groups I want to do operations/statistics on
So what I want the user to do is to use a function like:
test_function(data = dat, time_var = "time", group_vars = c("group1", "group3"))
Note, I might choose different grouping variables or none next time.
Let's say within the function I want to:
- calculate certain statistics on the time variable, e.g. the quantiles. Note: I want to split this up by my grouping variables
Here's one of my latest tries:
test_function <- function(data, time_var = NULL, group_vars = NULL)
{
# Note I initialize the variables with NULL, since e.g. the user might not specify a grouping
and I want to check for that in my function at some point)
time_var <- enquo(time_var)
group_vars <- enquos(group_vars)
# Here I try to group by my grouping variables
temp_data <- data %>%
group_by_at(group_vars) %>%
mutate(!!sym(time_var) := !!sym(time_var) / 60)
# Here I'm calculating some stats
time_stats <- temp_data %>%
summarize_at(vars(!!time_var), list(p0.1_time = ~quantile(., probs = 0.1, na.rm = T),
p0.2_time = ~quantile(., probs = 0.2, na.rm = T),
p0.3_time = ~quantile(., probs = 0.3, na.rm = T),
p0.4_time = ~quantile(., probs = 0.4, na.rm = T),
p0.5_time = ~quantile(., probs = 0.5, na.rm = T),
p0.6_time = ~quantile(., probs = 0.6, na.rm = T),
p0.7_time = ~quantile(., probs = 0.7, na.rm = T),
p0.8_time = ~quantile(., probs = 0.8, na.rm = T),
p0.9_time = ~quantile(., probs = 0.9, na.rm = T),
p0.95_time = ~quantile(., probs = 0.95, na.rm = T)))
}
What is wrong with my code? I.e. I specifically struggle with the !!, !!!, sym, enquo, enquos things. Why does the group_by_at thing doesn't need the !! thing, whereas my summarize and mutate do need it?