7 Glossary

This glossary contains the vocabulary necessary to work with tidy evaluation and, more generally, with expressions. The definitions in rlang are generally consistent with base R. When they differ, both definitions are presented so you can navigate between these two worlds more easily.

7.1 Data structures

7.1.1 TODO Data mask

7.1.2 Expression

An expression is a piece of R code that represents a value or a computation:

Expressions are normally transient. They are computed (or evaluated) when you source a file or call a function. You can only observe:

  • The final value of the outermost expression.

  • Their side effects, such as the console output of a print() expression inside a loop.

In R however, it is possible to suspend the normal evaluation of expressions with the quotation mechanism. In a way, quotation causes expressions to freeze in place:

The technical definition of expressions is any R object that is created by parsing R code:

Unlike constants, symbols and calls are symbolic objects: their value depends on the environment.

7.1.3 Expression (base)

In base R, “expression” refers to a special type of vector that contains quoted expressions in the rlang sense:

You’ll most likely encounter this rare data structure as the return value of base::parse():

The only advantage of expression vectors compared to lists is that they include source references. Expression vectors with source references are printed with whitespace and comments preserved:

Source references are mostly useful for debugging and development tools. They don’t play any computational role and tidy evaluation doesn’t make use of references. Consequently the parsing tools in rlang return normal lists of expressions (in the rlang sense) instead of expression vectors:

7.1.4 TODO Symbol

7.2 Programming Concepts

7.2.1 Constant versus symbolic

Constants, also called “literals”, always have the same value no matter the context. On the other hand, symbols and calls are symbolic expressions: their value depends on an environment and what kind of objects are defined there.

For instance the string "mickey" always represents the same string no matter the environment and what objects are defined there:

In constrast, symbols depend on current definitions:

One source of problems when you’re working with quoted expressions is that they might be evaluated in arbitrary places, where objects have potentially been redefined to something different than expected. This is a common issue with tidyverse grammars because they evaluate quoted expressions in a data mask. Say you’d like to divide a column by a factor defined in the current environment:

This works fine but what if the data frame contains a column called factor? The expression will be evaluated with the parasite definition:

Masking is generally not a problem in scripts because you know what columns are inside your data frame. However as soon as your code is getting more general, for instance if you create a reusable function, you can no longer make assumptions about what’s in the data.

Fortunately with quasiquotation it is easy to solve masking issues by replacing symbols with constants. The unquoting operator !! allows you to inline constant values deep inside expressions. With qq_show() we can observe the inlining:

Because constants have the same value in any environment, the data mask can never take over with parasite definitions:

7.2.2 TODO Non-Standard Evaluation (NSE)

7.2.3 TODO Quotation versus Evaluation

7.2.4 TODO Quasiquotation

7.2.5 TODO Parsing

7.2.6 TODO Metaprogramming