rlang (version 0.0.0.9000)

as_tidy_quote: Coerce expressions to a tidy formula quote.

Description

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.

Usage

as_tidy_quote(x, env)
is_tidy_quote(x)

Arguments

x
An object to test or convert.
env
The environment for the returned tidy quote.

Details

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

Examples

Run this code
# 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())

Run the code above in your browser using DataLab