tidy_eval(~ 1 + 2 + 3)
# formulas automatically capture their enclosing environment
foo <- function(x) {
y <- 10
~ x + y
}
f <- foo(1)
f
tidy_eval(f)
# If you supply data, tidy_eval will look their first:
tidy_eval(~ cyl, mtcars)
# To avoid ambiguity, you can use .env and .data pronouns to be
# explicit:
cyl <- 10
tidy_eval(~ .data$cyl, mtcars)
tidy_eval(~ .env$cyl, mtcars)
# Imagine you are computing the mean of a variable:
tidy_eval(~ mean(cyl), mtcars)
# How can you change the variable that's being computed?
# The easiest way is "unquote" with !!
# See ?tidy_quote for more details
var <- ~ cyl
tidy_eval(tidy_quote(mean( !!var )), mtcars)
Run the code above in your browser using DataLab