Tidy Evaluation With Rlang::: Cheat Sheet
Tidy Evaluation With Rlang::: Cheat Sheet
rlang::caller_env(n = 1) Returns rlang::quo(expr) Quote contents as a quosure. Also quos to quote rlang::expr(expr) Quote contents. Also exprs to quote multiple
calling env of the function it is in. multiple expressions. a <- 1; b <- 2; q <- quo(a + b); qs <- quos(a, b) expressions. a <- 1; b <- 2; e <- expr(a + b); es <- exprs(a, b, a + b)
rlang::child_env(.parent, ...) Creates rlang::enquo(arg) Call from within a function to quote what the user rlang::enexpr(arg) Call from within a function to quote what the user
new env as child of .parent. Also env. passed to an argument as a quosure. Also enquos for multiple args. passed to an argument. Also enexprs to quote multiple arguments.
rlang::current_env() Returns quote_this < - function(x) enquo(x) quote_that < - function(x) enexpr(x)
execution env of the function it is in. quote_these < - function(…) enquos(…) quote_those < - function(…) enexprs(…)
Constant - a bare value (i.e. an atomic rlang::new_quosure(expr, env = caller_env()) Build a rlang::ensym(x) Call from within a function to quote what the user
1 quosure from a quoted expression and an environment. passed to an argument as a symbol, accepts strings. Also ensyms.
vector of length 1). is_bare_atomic(1)
new_quosure(expr(a + b), current_env()) quote_name < - function(name) ensym(name)
Call object - a vector of symbols/constants/calls quote_names < - function(…) ensyms(…)
abs ( 1 ) that begins with a function name, possibly
followed by arguments. is_call(expr(abs(1)))
RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at tidyeval.tidyverse.org • rlang 0.3.0 • Updated: 2018-11
Quasiquotation (!!, !!!, :=) Programming Recipes WRITE A
FUNCTION
THAT RECOGNIZES
Quoting function- A function that quotes any of its arguments internally for delayed evaluation QUASIQUOTATION
QUOTATION QUASIQUOTATION in a chosen environment. You must take special steps to program safely with a quoting function. (!!,!!!,:=)
Storing an expression Quoting some parts of an
without evaluating it. expression while evaluating 1. Capture the
How to spot a quoting function? dplyr::filter(cars, speed = = 25) quasiquotation-aware
e <- expr(a + b) and then inserting the results argument with rlang::enquo.
of others (unquoting others). A function quotes an argument if the speed dist
e <- expr(a + b) argument returns an error when run
on its own. 1 25 85 2. Evaluate the arg with rlang::eval_tidy.
e
Many tidyverse functions are quoting add1 <- function(x) {
log ( e ) log(e) log ( a + b ) log(a + b) speed == 25 q <- rlang::enquo(x)
functions: e.g. filter, select, mutate, 1
fun a+b fun summarise, etc. Error! rlang::eval_tidy(q) + 1 2
}
expr(log(e)) expr(log(!!e))
PROGRAM WITH A QUOTING FUNCTION PASS MULTIPLE ARGUMENTS PASS TO ARGUMENT NAMES
rlang provides !!, !!!, and := for doing quasiquotation. TO A QUOTING FUNCTION OF A QUOTING FUNCTION
!!, !!!, and := are not functions but syntax (symbols recognized
data_mean <- function(data, var) { group_mean <- function(data, var, …) { named_mean <- function(data, var) {
by the functions they are passed to). Compare this to how
require(dplyr) require(dplyr) require(dplyr)
. is used by magrittr::%>%() var <- rlang::enquo(var) 1 var <- rlang::enquo(var) var <- rlang::ensym(var) 1
. is used by stats::lm() data %>% group_vars <- rlang::enquos(…) 1 data %>%
.x is used by purrr::map(), and so on. summarise(mean = mean(!!var)) 2 data %>% summarise(!!name := mean(!!var)) 2
} group_by(!!!group_vars) %>% 2 }
!!, !!!, and := are only recognized by some rlang functions and
functions that use those functions (such as tidyverse functions). summarise(mean = mean(!!var))
}
a !! Unquotes the 1. Capture user argument that will 1. Capture user arguments that will 1. Capture user argument that will
symbol or call that be quoted with rlang::enquo. be quoted with rlang::enquos. be quoted with rlang::ensym.
log ( 1 + b ) log(1 + b) follows. Pronounced
fun fun 2 "unquote" or "bang- 2. Unquote the user argument into 2. Unquote splice the user arguments 2. Unquote the name into the
!! the quoting function with !!. into the quoting function with !!!. quoting function with !! and :=.
bang." a <- 1; b <- 2
expr(log(!!a + b)) expr(log(!!a + b))
RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at tidyeval.tidyverse.org • rlang 0.3.0 • Updated: 2018-11