Tidy evaluation in R, or tidy eval for short, is a pretty complex topic. But for some specific uses, it's not all that complex. One important task that tidy eval helps handle is both useful and easy: incorporating functions from packages such as dplyr and ggplot2 inside your own custom functions.

Let me go through an example. Using the ubiquitous mtcars sample data set, here’s how I might do a scatterplot of miles per gallon by weight, using dplyr to filter for higher-mpg cars only (I’ve also added a few style tweaks):

library(ggplot2)
library(ggthemes)
library(dplyr)
library(rlang)
graph_title <- "MPG by Weight (thousands lbs) for higher MPG cars"
filter(mtcars, mpg >= 20) %>%
ggplot(aes(x=wt, y=mpg)) +
geom_point(color = "darkblue") +
theme_hc() + xlab("") + ylab("") +
ggtitle(graph_title) +
theme(plot.title = element_text(size = 14, hjust = 0.5)) +
geom_smooth(method = 'lm', formula = y~x, se = FALSE, linetype = "dotted", color = "darkgrey")

Next, I’d like to create a function with this code, so I can easily reuse these customizations to plot other data, not just mtcars. That's where problems arise that tidy eval can solve.

To read this article in full, please click here