ABSTRACT

Chapter 17 describes the big picture on metaprogramming. Metaprogramming 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 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!

Each section in this chapter introduces one big new idea: “Code is data” shows that code is data and teaches you how to create and modify expressions by capturing code. “Code is a tree” describes the tree-like structure of code, called an abstract syntax tree. “Code can generate code” shows how to create new expressions programmatically. “Evaluation runs code” shows how to execute expressions by evaluating them in an environment. “Customising evaluation with functions” illustrates how to customise evaluation by supplying custom functions in a new environment. “Customising evaluation with data” extends that customisation to data masks, which blur the line between environments and data frames. “Quosures” introduces a new data structure called the quosure that makes all this simpler and more correct.