Examples for 'rlang::defusing-advanced'


Advanced defusal operators

Aliases: defusing-advanced enexpr exprs enexprs ensym ensyms quo quos enquo0 enquos0

Keywords: internal

### ** Examples

# `exprs()` is the plural variant of `expr()`
exprs(foo, bar, bar)
[[1]]
foo

[[2]]
bar

[[3]]
bar
# `quo()` and `quos()` are the quosure variants of `expr()` and `exprs()`
quo(foo)
<quosure>
expr: ^foo
env:  0x55ccfe0f9bb0
quos(foo, bar)
<list_of<quosure>>

[[1]]
<quosure>
expr: ^foo
env:  0x55ccfe0f9bb0

[[2]]
<quosure>
expr: ^bar
env:  0x55ccfe0f9bb0
# `enexpr()` and `enexprs()` are the naked variants of `enquo()` and `enquos()`
my_function1 <- function(arg) enexpr(arg)
my_function2 <- function(arg, ...) enexprs(arg, ...)
my_function1(1 + 1)
1 + 1
my_function2(1 + 1, 10 * 2)
[[1]]
1 + 1

[[2]]
10 * 2
# `ensym()` and `ensyms()` are symbol variants of `enexpr()` and `enexprs()`
my_function3 <- function(arg) ensym(arg)
my_function4 <- function(arg, ...) ensyms(arg, ...)

# The user must supply symbols
my_function3(foo)
foo
my_function4(foo, bar)
[[1]]
foo

[[2]]
bar
# Complex expressions are an error
try(my_function3(1 + 1))
Error in ensym(arg) : Can't convert to a symbol.
try(my_function4(1 + 1, 10 * 2))
Error in sym(expr) : Can't convert a call to a symbol.
# `enquo0()` and `enquos0()` disable injection operators
automatic_injection <- function(x) enquo(x)
no_injection <- function(x) enquo0(x)

automatic_injection(foo(!!!1:3))
<quosure>
expr: ^foo(1L, 2L, 3L)
env:  0x55ccfe0f9bb0
no_injection(foo(!!!1:3))
<quosure>
expr: ^foo(!!!1:3)
env:  0x55ccfe0f9bb0
# Injection can still be done explicitly
inject(no_injection(foo(!!!1:3)))
<quosure>
expr: ^foo(1L, 2L, 3L)
env:  0x55ccfe0f9bb0

[Package rlang version 1.1.4 Index]