Quosure objects wrap an expression with a lexical
enclosure. This is a powerful quoting (see
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
as_quosure() is useful for functions that expect quosures but
allow specifying a raw expression as well. It has two possible
x is not a quosure, it wraps it into a quosure
env as scope. If
x is an unscoped quosure (see
env is used as a default scope. On the other
x has a valid enclosure, it is returned as is (even if
env is not the same as the formula environment).
as_quosure(x, env = caller_env()) as_quosureish(x, env = caller_env())
An object to convert.
An environment specifying the lexical enclosure of the quosure.
# Sometimes you get unscoped formulas because of quotation: f <- ~~expr inner_f <- f_rhs(f) str(inner_f)#> language ~expris_quosureish(inner_f, scoped = TRUE)#>  FALSE# You can use as_quosure() to provide a default environment: as_quosure(inner_f, base_env())#> <quosure: base> #> ~~expr# Or convert expressions or any R object to a validly scoped quosure: as_quosure(quote(expr), base_env())#> <quosure: base> #> ~expras_quosure(10L, base_env())#> <quosure: empty> #> ~10L# While as_quosure() always returns a quosure (one-sided formula), # as_quosureish() returns quosureish objects: as_quosure(a := b)#> <quosure: local> #> ~`:=`(a, b)as_quosureish(a := b)#> <quosure: local> #> ~`:=`(a, b)as_quosureish(10L)#> <quosure: local> #> ~10L