cement <- function(...) { args <- rlang::ensyms(...) paste( purrr::map(args, rlang::as_string), collapse = " " )}
cement <- function(...) { args <- rlang::ensyms(...) paste( purrr::map(args, rlang::as_string), collapse = " " )}
canonical <- paste( letters[c(3, 15, 14, 20, 18, 9, 22, 5, 4)], collapse = "")cement( This, example, is, !!canonical)
cement <- function(...) { args <- rlang::ensyms(...) paste( purrr::map(args, rlang::as_string), collapse = " " )}
canonical <- paste( letters[c(3, 15, 14, 20, 18, 9, 22, 5, 4)], collapse = "")cement( This, example, is, !!canonical)
## [1] "This example is contrived"
# Evaluatedmean(1:5)
## [1] 3
1:5
## [1] 1 2 3 4 5
# Evaluatedmean(1:5)
## [1] 3
1:5
## [1] 1 2 3 4 5
# Quotedlibrary(rlang)
rlang#> Error: object 'rlang' not found
==
==
==
==
==
==
Kinda. Technically NSE is about arguments only.
library(MASS)
mtcars2 <- subset(mtcars, cyl == 4)
with(mtcars2, sum(vs))
sum(mtcars2$am)
library(MASS)
mtcars2 <- subset(mtcars, cyl == 4)
with(mtcars2, sum(vs))
sum(mtcars2$am)
library(dplyr, warn.conflicts = FALSE)
library(ggplot2, warn.conflicts = FALSE)
byCyl <- mtcars %>%
group_by(cyl) %>%
summarise(mean = mean(mpg), .groups = "drop_last")
ggplot(byCyl, aes(cyl, mean)) + geom_point()
library(dplyr, warn.conflicts = FALSE)
library(ggplot2, warn.conflicts = FALSE)
byCyl <- mtcars %>%
group_by(cyl) %>%
summarise(mean = mean(mpg), .groups = "drop_last")
ggplot(byCyl, aes(cyl, mean)) + geom_point()
Note: The first mean
shouldn't be highlighted, but I'm struggling with {flair} 😦
{rlang}
expr
vs enexpr
exprs
vs enexprs
quote directly
vs quote the thing in the calling environment
testing <- "foo"testing2 <- testingrlang::expr(testing)
## testing
rlang::expr(testing2)
## testing2
testing <- "foo"testing2 <- testingrlang::expr(testing)
## testing
rlang::expr(testing2)
## testing2
rlang::enexpr(testing)
## [1] "foo"
catch_it <- function(x) rlang::enexpr(x)catch_it(testing)
## testing
catch_it(testing2)
## testing2
exprs
and enexprs
capture a list of expr
or enexpr
rlang::exprs(x = testing, y = testing2, z = paste(testing, testing2))
## $x## testing## ## $y## testing2## ## $z## paste(testing, testing2)
# shorthand for# list(# x = expr(testing), # y = expr(testing2), # z = expr(paste(testing, testing2))# )
sym
and syms
make sure the thing they're capturing is symbol or characterensym
and ensyms
in functionsexpr
is to quote
enexpr
is to substitute
("normal" usage)exprs
is to alist
enexprs(...)
is to as.list(substitute(...()))
???substitute
example: library
library_meat <- function(package) { as.character(substitute(package)) # Without as.character, it returns a name}library_meat(rlang)
## [1] "rlang"
substitute
can be used to... substitutef4 <- function(x) substitute(x * 2)f4(a + b + c)
## (a + b + c) * 2
f4(whatever)
## whatever * 2
substitute
can be used to... substitutef4b <- function(x) substitute(x, list(a = 1, b = 2, c = 3))f4b(a + b + c) # Not what I meant!
## x
f4b(literally - anything)
## x
x <- expr(-1)y <- "a character"rlang::expr(f(!!x, y))
## f(-1, y)
rlang::expr(f(x, !!y))
## f(x, "a character")
rlang::expr(mean(1:3) + mean(4:6))
## mean(1:3) + mean(4:6)
rlang::expr(!!mean(1:3) + !!mean(4:6))
## 2 + 5
replace_f <- function(func) { f <- rlang::enexpr(func) rlang::expr((!!f)(x, y)) # Would be better if we used the actual formals...}replace_f(mean)
## mean(x, y)
replace_f(rlang::enexpr)
## rlang::enexpr(x, y)
!!!
is to !!
as exprs
is to expr
multi_arg <- list(a = 1, b = 2, c = "other")rlang::expr(f(!!!multi_arg, another_arg))
## f(a = 1, b = 2, c = "other", another_arg)
!!
and !!!
don't actually existrlang::`!!`
## function (x) ## {## abort("`!!` can only be used within a quasiquoted argument")## }## <bytecode: 0x000000001347dcc8>## <environment: namespace:rlang>
rlang::`!!!`
## function (x) ## {## abort("`!!!` can only be used within a quasiquoted argument")## }## <bytecode: 0x0000000016ad5590>## <environment: namespace:rlang>
...
per se:=
"colon-equals" or "digested is" (how I think of it)=
can't be evaluated, so we trick Rvar <- "my_var_name"val <- 1:3tibble::tibble(!!var := val)
## # A tibble: 3 x 1## my_var_name## <int>## 1 1## 2 2## 3 3
rlang::exec
is similar to base::do.call
params <- list(na.rm = TRUE, trim = 0.1)func <- "mean"rlang::exec(func, x = 1:10, !!!params)
## [1] 5.5
stringr::str_replace(string, pattern, replacement)
funky::fn_replace(function, pattern, replacement)
rlang
or quote
or substitute
(etc)Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |