I am working with R Shiny for some exploratory data analysis. I have two checkbox inputs that contain only the user-selected options. The first checkbox input contains only
You can use the helpers from rlang
package, which is created by the same team that created dplyr
. When using dplyr
and other tidyverse packages, you don't have to load the rlang
packages in order to use those helpers.
Specifically, you can use the syms
function and the !!!
function like so:
library(dplyr)
group_cols <- c("vs", "am")
mtcars %>%
group_by(!!!syms(group_cols)) %>%
summarize(mean_wt = mean(wt))
This closely-related question and answer explains how the !!
operator and sym
function are used for a single column name (i.e. a length-one character vector).
With dplyr 1.0.0
, we have the following possibility based on the "normal" group_by
:
library(dplyr)
group_cols <- c("vs", "am")
mtcars %>%
group_by(across(all_of(group_cols))) %>%
summarize(mean_wt = mean(wt))
If you have a vector of variable names, you should pass them to the .dots=
parameter of group_by_
. For example:
mtcars %>%
group_by_(.dots=c("mpg","hp","wt")) %>%
summarize(x=mean(gear))
Recent versions of the dplyr
package include variants of group_by
, such as group_by_if
and group_by_at
. You can use these to perform column selections with syntax that is similar to the select
function.
Just as you could select a list of columns with select(my_data, one_of(group_cols))
, you can use group_by_at
to do the following:
library(dplyr)
group_cols <- c("vs", "am")
mtcars %>%
group_by_at(.vars = vars(one_of(group_cols))) %>%
summarize(mean_wt = mean(wt))