Why I find tidyeval usefulR
First thing’s first: maybe you shouldn’t care about
tidyeval. Maybe you don’t need it. If you
exclusively work interactively, I don’t think that learning about
tidyeval is important. I can
only speak for me, and explain to you why I personally find
But maybe now you know how to use it, but not why and when you should use it… Basically, whenever you want to write a function that looks something like this:
is when you want to use the power of
I work at STATEC, Luxembourg’s national institute of statistics. I work on all kinds of different projects, and when data gets updated (for example because a new round of data collection for some survey finished), I run my own scripts on the fresh data to make the data nice and tidy for analysis. Because surveys get updated, sometimes column names change a little bit, and this can cause some issues.
Very recently, a dataset I work with got updated. Data collection was finished, so I
just loaded my hombrewed package written for this project, changed the path from last year’s script
to this year’s fresh data path, ran the code, and watched as the folders got populated with new
ggplot2 graphs and LaTeX tables with descriptive statistics and regression
results. This is then used to generate this year’s report. However, by looking at the graphs, I
noticed something weird; some graphs were showing some very strange patterns. It turns out that one
column got its name changed, and also one of its values got changed too.
Last year, this column, let’s call it
spam, had values
This year the column is called
Spam and the values are
2. When I found out that this was
the source of the problem, I just had to change the arguments of my functions from
generate_spam_plot(dataset = data2016, column = spam, value = 1) generate_spam_plot(dataset = data2016, column = spam, value = 0)
generate_spam_plot(dataset = data2017, column = Spam, value = 1) generate_spam_plot(dataset = data2017, column = Spam, value = 2)
without needing to change anything else. This is why I use
tidyeval; without it, writing a
function such as
genereta_spam_plot would not be easy. It would be possible, but not easy.
If you want to know more about
tidyeval and working programmatically with R, I shamelessly
invite you to read a book I’ve been working on: https://b-rodrigues.github.io/fput/
It’s still a WIP, but maybe you’ll find it useful. I plan on finishing it by the end of the year,
but there’s already some content to keep you busy!