Learn R Programming

rlang (version 0.1)

as_quosure: Coerce object to quosure

Description

Quosure objects wrap an expression with a lexical enclosure. This is a powerful quoting (see base::quote() and quo()) mechanism that makes it possible to carry and manipulate expressions while making sure that its symbolic content (symbols and named calls, see is_symbolic()) is correctly looked up during evaluation.

  • new_quosure() creates a quosure from a raw expression and an environment.

  • as_quosure() is useful for functions that expect quosures but allow specifying a raw expression as well. It has two possible effects: if x is not a quosure, it wraps it into a quosure bundling env as scope. If x is an unscoped quosure (see is_quosure()), env is used as a default scope. On the other hand if x has a valid enclosure, it is returned as is (even if env is not the same as the formula environment).

  • While as_quosure() always returns a quosure (a one-sided formula), even when its input is a formula or a definition, as_quosureish() returns quosureish inputs as is.

Usage

as_quosure(x, env = caller_env())

as_quosureish(x, env = caller_env())

Arguments

x

An object to convert.

env

An environment specifying the lexical enclosure of the quosure.

See Also

is_quosure()

Examples

Run this code
# NOT RUN {
# Sometimes you get unscoped formulas because of quotation:
f <- ~~expr
inner_f <- f_rhs(f)
str(inner_f)
is_quosureish(inner_f, scoped = TRUE)

# You can use as_quosure() to provide a default environment:
as_quosure(inner_f, base_env())

# Or convert expressions or any R object to a validly scoped quosure:
as_quosure(quote(expr), base_env())
as_quosure(10L, base_env())


# While as_quosure() always returns a quosure (one-sided formula),
# as_quosureish() returns quosureish objects:
as_quosure(a := b)
as_quosureish(a := b)
as_quosureish(10L)
# }

Run the code above in your browser using DataLab