Title: | Build Formulas Using Tidy Selection Helpers |
---|---|
Description: | Provides the function 'tidyformula()', which translates formulas containing 'tidyselect'-style selection helpers. It expands these helpers by evaluating 'dplyr::select()' with the relevant selection helper and a supplied data frame. The package contains methods for traversing abstract syntax trees from Wickham, Hadley (2019) <doi:10.1201/9781351201315>. |
Authors: | Damian Pavlyshyn [aut, cre] |
Maintainer: | Damian Pavlyshyn <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2025-03-08 03:30:21 UTC |
Source: | https://github.com/damian-t-p/tidyformula |
tidyselect
-style selection helperstidyformula()
translates formulas containing tidyselect
-style
selection helpers, expanding these helpers by evaluating
dplyr::select()
with the relevant selection helper and a supplied data frame.
When the selection helper appears as the first argument of a function, that function is distributed across the sum of the selected variables.
tidyformula( formula, data, select_helpers = .select_helpers, nodistribute = c("+", ":", "*", "^"), env = rlang::caller_env() )
tidyformula( formula, data, select_helpers = .select_helpers, nodistribute = c("+", ":", "*", "^"), env = rlang::caller_env() )
formula |
An object of class |
data |
A data frame whose column names should be used for selection |
select_helpers |
A character vector. The names of selection helpers to be matched and substituted. |
nodistribute |
A character vector. Functions with these names are not distributed over selection helpers. |
env |
The environment to associate with the result. |
An object of class formula
, which is a translation of the argument
formula
in which the selection helpers are replaced with the corresponding
variables of data
.
df <- data.frame( x1 = rnorm(5), x2 = rnorm(5), x3 = rnorm(5), y = rnorm(5) ) tidyformula(y ~ num_range("x", 1:2) + z, data = df) #> y ~ x1 + x2 + z #> <environment: 0x000001fdee2dba08> tidyformula(y ~ poly(starts_with("x"), 3), data = df) #> y ~ poly(x1, 3) + poly(x2, 3) + poly(x3, 3) #> <environment: 0x000001fdee2dba08> tidyformula( ~ everything() * contains("x"), data = df) #> ~(x1 + x2 + x3 + y) * (x1 + x2 + x3) #> <environment: 0x000001fdee2dba08>
Interaction operators are typically not distributed, but this behaviour can be changed.
tidyformula(y ~ starts_with("x")^2, data = df) #> y ~ (x1 + x2 + x3)^2 #> <environment: 0x000001fdee2dba08> tidyformula(y ~ starts_with("x")^2, data = df, nodistribute = c("+", "-")) #> y ~ x1^2 + x2^2 + x3^2 #> <environment: 0x000001fdee2dba08>
dplyr::select()
, tidyselect::language
for
documentation of selection helpers.