Coerce expressions to a tidy formula quote.
A tidy quote is a formula or definition (see
op-definition) that bundles an expression and an
environment. In some situations a formula will not be a tidy quote
because it does not carry environment information. That happens for
instance when you quote a formula, e.g. in this snippet the outer
formula is a tidy quote but not the inner one:
is_formula() will return
for those degenerate formulas but
FALSE. Note that in the tidy evaluation framework
tidy_eval()), untidy formulas are automatically
given the environment of the outer formula and do not require
special actions on your part.
- An object to test or convert.
- The environment for the returned tidy quote.
as_tidy_quote() is useful for SE functions that expect a
tidy formula quote but allow specifying a raw expression as
well. It has two possible effects: if
x is not a formula, it
wraps it into a formula with
x is a
degenerate formula, it turns it into a tidy quote by adding
env. Finally, if
x is a tidy quote, it is left alone
env is not the same as the formula environment).
# Degenerate formulas are often created by quoting, since `~` # records the environment when it is evaluated the first time: f <- ~~expr # The outer formula has been evaluated and is a tidy quote: is_tidy_quote(f) # But the inner formula is not: inner_f <- f_rhs(f) is_tidy_quote(inner_f) # You can use as_tidy_quote() to add the environment information: as_tidy_quote(inner_f, base_env()) # Or turn expressions or any R object in a tidy quote: as_tidy_quote(quote(expr), env()) as_tidy_quote(10L, env())