12 Introduction

At some point, you might have found yourself copy-and-pasting the same dplyr code over and over again. For example, the following code uses group_by() and summarize() to calculate different grouped means of mpg.

A function would cut down on duplicated code. Here’s a first pass:

Unfortunately, it doesn’t work.

dplyr verbs and other tidyverse functions that operate on data frames work differently than many other R functions. In this section, you’ll learn what makes these functions different and figure out how to make functions like grouped_mean() work. The underlying idea that makes this all possible is called tidy evaluation.

Tidy evaluation is a complicated subject. Luckily, you don’t need to understand all the theory or the under-the-hood mechanics to use tidy evaluation. The Basics chapter is a high-level, practical overview of the basic tidy evaluation use-cases. The goal of the chapter is to show you what to do without going into much theory.

If you’re curious tidy evaluation and want to learn more, the following are useful resources. Note that the {{ }} operator, which you’ll learn about in the next chapter, was not implemented when many of these were written. Instead of {{ }}, you’ll usually see !! and enquo().

And if you really want to go back to the beginning: