| enquo | R Documentation |
enquo() and enquos() defuse function arguments.
A defused expression can be examined, modified, and injected into
other expressions.
Defusing function arguments is useful for:
Creating data-masking functions.
Interfacing with another data-masking function using the defuse-and-inject pattern.
These are advanced tools. Make sure to first learn about the embrace
operator {{ in Data mask programming patterns.
{{ is easier to work with less theory, and it is sufficient
in most applications.
enquo(arg)
enquos(
...,
.named = FALSE,
.ignore_empty = c("trailing", "none", "all"),
.ignore_null = c("none", "all"),
.unquote_names = TRUE,
.homonyms = c("keep", "first", "last", "error"),
.check_assign = FALSE
)
arg |
An unquoted argument name. The expression supplied to that argument is defused and returned. |
... |
Names of arguments to defuse. |
.named |
If |
.ignore_empty |
Whether to ignore empty arguments. Can be one
of |
.ignore_null |
Whether to ignore unnamed null arguments. Can be
|
.unquote_names |
Whether to treat |
.homonyms |
How to treat arguments with the same name. The
default, |
.check_assign |
Whether to check for |
enquo() returns a quosure and enquos()
returns a list of quosures.
Arguments defused with enquo() and enquos() automatically gain
injection support.
my_mean <- function(data, var) {
var <- enquo(var)
dplyr::summarise(data, mean(!!var))
}
# Can now use `!!` and `{{`
my_mean(mtcars, !!sym("cyl"))
See enquo0() and enquos0() for variants that don't enable
injection.
Defusing R expressions for an overview.
expr() to defuse your own local expressions.
Advanced defusal operators.
base::eval() and eval_bare() for resuming evaluation
of a defused expression.
# `enquo()` defuses the expression supplied by your user
f <- function(arg) {
enquo(arg)
}
f(1 + 1)
# `enquos()` works with arguments and dots. It returns a list of
# expressions
f <- function(...) {
enquos(...)
}
f(1 + 1, 2 * 10)
# `enquo()` and `enquos()` enable _injection_ and _embracing_ for
# your users
g <- function(arg) {
f({{ arg }} * 2)
}
g(100)
column <- sym("cyl")
g(!!column)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.