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: ~~expr. is_formula() will return TRUE for those degenerate formulas but is_tidy_quote() will return FALSE. Note that in the tidy evaluation framework (see tidy_eval()), untidy formulas are automatically given the environment of the outer formula and do not require special actions on your part.

as_tidy_quote(x, env)
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 env. If 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 (even if env is not the same as the formula environment).

  • as_tidy_quote
  • is_tidy_quote
# 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:

# But the inner formula is not:
inner_f <- f_rhs(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())
Documentation reproduced from package rlang, version, License: GPL-3

Community examples

Looks like there are no examples yet.