17.1 Introduction

Metaprogramming is the hardest topic in this book because it brings together many formerly unrelated topics and forces you grapple with issues that you probably haven’t thought about before. You’ll also need to learn a lot of new vocabulary, and at first it will seem like every new term is defined by three other terms that you haven’t heard of. Even if you’re an experienced programmer in another language, your existing skills are unlikely to be much help as few modern popular languages expose the level of metaprogramming that R provides. So don’t be surprised if you’re frustrated or confused at first; this is a natural part of the process that happens to everyone!

But I think it’s easier to learn metaprogramming now than ever before. Over the last few years, the theory and practice have matured substantially, providing a strong foundation paired with tools that allow you to solve common problems. In this chapter, you’ll get the big picture of all the main pieces and how they fit together.

Outline

Each section in this chapter introduces one big new idea:

  • Section 17.2 shows that code is data and teaches you how to create and modify expressions by capturing code.

  • Section 17.3 describes the tree-like structure of code, called an abstract syntax tree.

  • Section 17.4 shows how to create new expressions programmatically.

  • Section 17.5 shows how to execute expressions by evaluating them in an environment.

  • Section 17.6 illustrates how to customise evaluation by supplying custom functions in a new environment.

  • Section 17.7 extends that customisation to data masks, which blur the line between environments and data frames.

  • Section 17.8 introduces a new data structure called the quosure that makes all this simpler and more correct.

Prerequisites

This chapter introduces the big ideas using rlang; you’ll learn the base equivalents in later chapters. We’ll also use the lobstr package to explore the tree structure of code.

library(rlang)
library(lobstr)

Make sure that you’re also familiar with the environment (Section 7.2) and data frame (Section 3.6) data structures.