Skip to contents

Parse a formula into its terms lhs ~ rhs where rhs can have two components separated by |. For example, y ~ x | z has rhs = ~ x | z where x is typically described as the grouping term and z is the blocking term. A fast, unsafe alternative to modeltools::ParseFormula().

Usage

parse_formula(formula, specials = NULL, data = NULL, terms = FALSE)

Arguments

formula

(formula)
The formula that should be parsed.

specials

(character: NULL)
A character vector of functions in formula that should be flagged in the specials attribute when terms.formula() is applied. Has no effect unless expansion runs.

data

(data frame)
Used to infer the meaning of the special symbol . in formula. Can be a data.frame or list.

terms

(scalar logical: FALSE)
Whether or not to parse formula using stats::terms.formula(). Expansion is also performed when specials or data is non-NULL, even if terms is FALSE.

Value

A named list with components:

  • formula: the (possibly terms.formula-expanded) input formula.

  • lhs: a one-sided formula containing the response, or NULL if formula is one-sided.

  • rhs: a one-sided formula containing the full right-hand side.

  • rhs_group: a one-sided formula containing the term left of |, or rhs if no top-level | is present.

  • rhs_block: a one-sided formula containing the term right of |, or NULL if no top-level | is present.

Examples

#----------------------------------------------------------------------------
# parse_formula() example
#----------------------------------------------------------------------------
library(bkmodel)

# One-sided formula
parse_formula(~x)
#> $formula
#> ~x
#> <environment: 0x5ed292892260>
#> 
#> $lhs
#> NULL
#> 
#> $rhs
#> ~x
#> <environment: 0x5ed292892260>
#> 
#> $rhs_group
#> ~x
#> <environment: 0x5ed292892260>
#> 
#> $rhs_block
#> NULL
#> 

# Two-sided formula
parse_formula(y ~ x)
#> $formula
#> y ~ x
#> <environment: 0x5ed292892260>
#> 
#> $lhs
#> ~y
#> <environment: 0x5ed292892260>
#> 
#> $rhs
#> ~x
#> <environment: 0x5ed292892260>
#> 
#> $rhs_group
#> ~x
#> <environment: 0x5ed292892260>
#> 
#> $rhs_block
#> NULL
#> 

# Two-sided formula with a top-level `|` splitting the rhs into group and block
parse_formula(y ~ x | z)
#> $formula
#> y ~ x | z
#> <environment: 0x5ed292892260>
#> 
#> $lhs
#> ~y
#> <environment: 0x5ed292892260>
#> 
#> $rhs
#> ~x | z
#> <environment: 0x5ed292892260>
#> 
#> $rhs_group
#> ~x
#> <environment: 0x5ed292892260>
#> 
#> $rhs_block
#> ~z
#> <environment: 0x5ed292892260>
#> 

# Dot expansion against the names in `data`
parse_formula(y ~ ., data = data.frame(y = 1, a = 1, b = 1))
#> $formula
#> y ~ a + b
#> <environment: 0x5ed292892260>
#> 
#> $lhs
#> ~y
#> <environment: 0x5ed292892260>
#> 
#> $rhs
#> ~a + b
#> <environment: 0x5ed292892260>
#> 
#> $rhs_group
#> ~a + b
#> <environment: 0x5ed292892260>
#> 
#> $rhs_block
#> NULL
#> 

# Functions named in `specials` are preserved through term expansion
parse_formula(y ~ s(x) + z, specials = "s")
#> $formula
#> y ~ s(x) + z
#> <environment: 0x5ed292892260>
#> 
#> $lhs
#> ~y
#> <environment: 0x5ed292892260>
#> 
#> $rhs
#> ~s(x) + z
#> <environment: 0x5ed292892260>
#> 
#> $rhs_group
#> ~s(x) + z
#> <environment: 0x5ed292892260>
#> 
#> $rhs_block
#> NULL
#>