MyNixOS website logo
Description

Build Formulas Using Tidy Selection Helpers.

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>.

tidyformula

R-CMD-check codecov

tidyformula() translates formulas containing tidyselect-style selection helpers, expanding these helpers by evaluating dplyr::select() with the relevant selection helper on a supplied data frame.

Installation

You can install the development version of tidyformula from GitHub with:

# install.packages("devtools")
devtools::install_github("damian-t-p/tidyformula")

Example

We demonstrate how to build formulas from the variables of the following toy data frame:

library(tidyformula)

df <- data.frame(
   x1 = rnorm(5),
   x2 = rnorm(5),
   x3 = rnorm(5),
   y  = rnorm(5)
 ) 

The simplest usage is adding a selection of variables. The tidy-selected variables can be combined with other variables in the formula:

tidyformula(y ~ starts_with("x") + z, data = df)
#> y ~ x1 + x2 + x3 + z
#> <environment: 0x00000202ad6c3730>

The selection helper can have additional arguments, as with num_range

tidyformula(y ~ num_range("x", 1:2) + z, data = df)
#> y ~ x1 + x2 + z
#> <environment: 0x00000202ad6c3730>

When the selection helper appears as the first argument of a function, that function is distributed across the sum of the selected variables.

This works with single-argument functions

tidyformula(y ~ log(contains("x")), data = df)
#> y ~ log(x1) + log(x2) + log(x3)
#> <environment: 0x00000202ad6c3730>

as well as multiple-argument ones.

tidyformula(y ~ poly(contains("x"), 3), data = df)
#> y ~ poly(x1, 3) + poly(x2, 3) + poly(x3, 3)
#> <environment: 0x00000202ad6c3730>

The functions +, -, *, and ^ are not distributed by default.

tidyformula( ~ everything()*z + starts_with("x")^2, data = df)
#> ~(x1 + x2 + x3 + y) * z + (x1 + x2 + x3)^2
#> <environment: 0x00000202ad6c3730>

This behaviour can be overwritten with the nodistribute argument, which is a character vector of functions that should not be distributed.

tidyformula( ~ everything()*z + starts_with("x")^2,
            data         = df,
            nodistribute = c("+", "-"))
#> ~x1 * z + x2 * z + x3 * z + y * z + (x1^2 + x2^2 + x3^2)
#> <environment: 0x00000202ad6c3730>
Metadata

Version

0.1.0

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows