Evaluate a formula

tidy_eval_rhs evaluates the RHS of a formula and tidy_eval_lhs evaluates the LHS. tidy_eval is a shortcut for tidy_eval_rhs since that is what you most commonly need.

tidy_eval_rhs(f, data = NULL)
tidy_eval_lhs(f, data = NULL)
tidy_eval(f, data = NULL)
data_source(x, lookup_msg = NULL)
A formula. Any expressions wrapped in UQ() will will be "unquoted", i.e. they will be evaluated, and the results inserted back into the formula. See tidy_quote() for more details.
A list (or data frame). data_source is a generic used to find the data associated with a given object. If you want to make tidy_eval work for your own objects, you can define a method for this generic.
An object for which you want to find associated data.
An error message when your data source is accessed inappropriately (by position rather than name).

If data is specified, variables will be looked for first in this object, and if not found in the environment of the formula.


When used with data, tidy_eval provides two pronouns to make it possible to be explicit about where you want values to come from: .env and .data. These are thin wrappers around .data and .env that throw errors if you try to access non-existent values.

  • tidy_eval
  • tidy_eval_rhs
  • tidy_eval_lhs
  • tidy_eval
  • data_source
tidy_eval(~ 1 + 2 + 3)

# formulas automatically capture their enclosing environment
foo <- function(x) {
  y <- 10
  ~ x + y
f <- foo(1)

# 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)
Documentation reproduced from package rlang, version, License: GPL-3

Community examples

Looks like there are no examples yet.