is_formula() tests whether
x is a call to
tests in addition that
x does not inherit from anything else than
Note: When we first implemented
is_formula(), we thought it
best to treat unevaluated formulas as formulas by default (see
section below). Now we think this default introduces too many edge
cases in normal code. We recommend always supplying
scoped = TRUE. Unevaluated formulas can be handled via a
is_formula(x, scoped = NULL, lhs = NULL) is_bare_formula(x, scoped = TRUE, lhs = NULL)
An object to test.
A boolean indicating whether the quosure is scoped,
that is, has a valid environment attribute and inherits from
A boolean indicating whether the formula has a left-hand
At parse time, a formula is a simple call to
~ and it does not
have a class or an environment. Once evaluated, the
becomes a properly structured formula. Unevaluated formulas arise
by quotation, e.g.
arg being supplied a formula. Use the
scoped argument to
check whether the formula carries an environment.
is_formula(~10)#>  TRUEis_formula(10)#>  FALSE# If you don't supply `lhs`, both one-sided and two-sided formulas # will return `TRUE` is_formula(disp ~ am)#>  TRUEis_formula(~am)#>  TRUE# You can also specify whether you expect a LHS: is_formula(disp ~ am, lhs = TRUE)#>  TRUEis_formula(disp ~ am, lhs = FALSE)#>  FALSEis_formula(~am, lhs = TRUE)#>  FALSEis_formula(~am, lhs = FALSE)#>  TRUE# Handling of unevaluated formulas is a bit tricky. These formulas # are special because they don't inherit from `"formula"` and they # don't carry an environment (they are not scoped): f <- quote(~foo) f_env(f)#> NULL# By default unevaluated formulas are treated as formulas is_formula(f)#>  TRUE# Supply `scoped = TRUE` to ensure you have an evaluated formula is_formula(f, scoped = TRUE)#>  FALSE# By default unevaluated formulas not treated as bare formulas is_bare_formula(f)#>  FALSE# If you supply `scoped = TRUE`, they will be considered bare # formulas even though they don't inherit from `"formula"` is_bare_formula(f, scoped = TRUE)#>  FALSE