# Selection interface -----------------------------------------------------
# A variable name held in a string, injected with !!
x <- "mpg"; y <- "wt"
mtcars %>% cor_test(!!rlang::sym(x), !!rlang::sym(y))
# Several names at once, spliced with !!!
vars <- c("mpg", "disp", "hp")
mtcars %>% cor_test(!!!rlang::syms(vars))
# A character vector via `vars =` (no rlang needed)
mtcars %>% cor_test(vars = c("mpg", "disp"))
# tidyselect helpers
iris %>% get_summary_stats(dplyr::all_of(c("Sepal.Length", "Sepal.Width")))
# Your own wrapper function: embrace the argument with {{ }}
my_summary <- function(data, var) {
data %>% get_summary_stats({{ var }}, type = "mean_sd")
}
my_summary(iris, Sepal.Length)
# Formula interface -------------------------------------------------------
# Build the formula from strings with reformulate(rhs, lhs)
outcome <- "len"; group <- "supp"
ToothGrowth %>% t_test(reformulate(group, outcome))
# The same inside a wrapper function
my_test <- function(data, outcome, group) {
data %>% t_test(reformulate(group, outcome))
}
my_test(ToothGrowth, "len", "supp")
# Programmatic grouping + a built formula (a common end-to-end pattern)
gv <- "supp"
ToothGrowth %>%
group_by(dplyr::across(dplyr::all_of(gv))) %>%
t_test(reformulate("dose", "len"))
Run the code above in your browser using DataLab