Tidy evaluation, most common actions

[This article was first published on That’s so Random, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Tidy evaluation is a bit challenging to get your head around. Even after reading programming with dplyr several times, I still struggle when creating functions from time to time. I made a small summary of the most common actions I perform, so I don’t have to dig in the vignettes and on stackoverflow over and over. Each is accompanied with a minimal example on how to implement it. I thought others might find this useful too, so here it is in a blog post. This list is meant to be a living thing so additions and improvements are most welcome. Please do a PR on this file or send an email.

<span class="n">library</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span><span class="w">
</span>

bare to quosure: quo

<span class="n">bare_to_quo</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">var</span><span class="p">){</span><span class="w">
  </span><span class="n">x</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">select</span><span class="p">(</span><span class="o">!!</span><span class="n">var</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">head</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">bare_to_quo</span><span class="p">(</span><span class="n">mtcars</span><span class="p">,</span><span class="w"> </span><span class="n">quo</span><span class="p">(</span><span class="n">cyl</span><span class="p">))</span><span class="w">
</span>
##           cyl
## Mazda RX4   6

bare to quosure in function: enquo

<span class="n">bare_to_quo_in_func</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">var</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">var_enq</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">enquo</span><span class="p">(</span><span class="n">var</span><span class="p">)</span><span class="w">
  </span><span class="n">x</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">select</span><span class="p">(</span><span class="o">!!</span><span class="n">var_enq</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">head</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">bare_to_quo_in_func</span><span class="p">(</span><span class="n">mtcars</span><span class="p">,</span><span class="w"> </span><span class="n">mpg</span><span class="p">)</span><span class="w">
</span>
##           mpg
## Mazda RX4  21

quosure to a name: quo_name

<span class="n">bare_to_name</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">nm</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">nm_name</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">quo_name</span><span class="p">(</span><span class="n">nm</span><span class="p">)</span><span class="w">
  </span><span class="n">x</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">mutate</span><span class="p">(</span><span class="o">!!</span><span class="n">nm_name</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="m">42</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">head</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">select</span><span class="p">(</span><span class="o">!!</span><span class="n">nm</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">bare_to_name</span><span class="p">(</span><span class="n">mtcars</span><span class="p">,</span><span class="w"> </span><span class="n">quo</span><span class="p">(</span><span class="n">this_is_42</span><span class="p">))</span><span class="w">
</span>
##   this_is_42
## 1         42

quosure to text: quo_text

<span class="n">quo_to_text</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">var</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">var_enq</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">enquo</span><span class="p">(</span><span class="n">var</span><span class="p">)</span><span class="w">
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">aes_string</span><span class="p">(</span><span class="n">rlang</span><span class="o">::</span><span class="n">quo_text</span><span class="p">(</span><span class="n">var_enq</span><span class="p">)))</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">geom_density</span><span class="p">()</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">plt</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">quo_to_text</span><span class="p">(</span><span class="n">mtcars</span><span class="p">,</span><span class="w"> </span><span class="n">cyl</span><span class="p">)</span><span class="w">
</span>

Note that tidy evaluation is not yet implemented in ggplot2, but this will be in future versions. This is a workaround for the meantime, when combining dplyr and ggplot2.

character to quosure: sym

<span class="n">char_to_quo</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">var</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">var_enq</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rlang</span><span class="o">::</span><span class="n">sym</span><span class="p">(</span><span class="n">var</span><span class="p">)</span><span class="w">
  </span><span class="n">x</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">select</span><span class="p">(</span><span class="o">!!</span><span class="n">var_enq</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">head</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">char_to_quo</span><span class="p">(</span><span class="n">mtcars</span><span class="p">,</span><span class="w"> </span><span class="s2">"vs"</span><span class="p">)</span><span class="w">
</span>
##           vs
## Mazda RX4  0

multiple bares to quosure: quos

<span class="n">bare_to_quo_mult</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">...</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">grouping</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">quos</span><span class="p">(</span><span class="n">...</span><span class="p">)</span><span class="w">
  </span><span class="n">x</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">group_by</span><span class="p">(</span><span class="o">!!!</span><span class="n">grouping</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">summarise</span><span class="p">(</span><span class="n">nr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="p">())</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">bare_to_quo_mult</span><span class="p">(</span><span class="n">mtcars</span><span class="p">,</span><span class="w"> </span><span class="n">vs</span><span class="p">,</span><span class="w"> </span><span class="n">cyl</span><span class="p">)</span><span class="w">
</span>
## # A tibble: 5 x 3
## # Groups:   vs [?]
##      vs   cyl    nr
##   <dbl> <dbl> <int>
## 1     0     4     1
## 2     0     6     3
## 3     0     8    14
## 4     1     4    10
## 5     1     6     4

multiple characters to quosure: syms

<span class="n">bare_to_quo_mult_chars</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">...</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">grouping</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rlang</span><span class="o">::</span><span class="n">syms</span><span class="p">(</span><span class="n">...</span><span class="p">)</span><span class="w">
  </span><span class="n">x</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">group_by</span><span class="p">(</span><span class="o">!!!</span><span class="n">grouping</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">summarise</span><span class="p">(</span><span class="n">nr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="p">())</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">bare_to_quo_mult_chars</span><span class="p">(</span><span class="n">mtcars</span><span class="p">,</span><span class="w"> </span><span class="nf">list</span><span class="p">(</span><span class="s2">"vs"</span><span class="p">,</span><span class="w"> </span><span class="s2">"cyl"</span><span class="p">))</span><span class="w">
</span>
## # A tibble: 5 x 3
## # Groups:   vs [?]
##      vs   cyl    nr
##   <dbl> <dbl> <int>
## 1     0     4     1
## 2     0     6     3
## 3     0     8    14
## 4     1     4    10
## 5     1     6     4

quoting full expressions

Altough quoting column names is most often used, it is by no means the only option. We can use the above to quote full expressions.

<span class="n">filter_func</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">filter_exp</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">filter_exp_enq</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">enquo</span><span class="p">(</span><span class="n">filter_exp</span><span class="p">)</span><span class="w">
  </span><span class="n">x</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">filter</span><span class="p">(</span><span class="o">!!</span><span class="n">filter_exp_enq</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">filter_func</span><span class="p">(</span><span class="n">mtcars</span><span class="p">,</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">93</span><span class="p">)</span><span class="w">
</span>
##    mpg cyl disp hp drat   wt  qsec vs am gear carb
## 1 22.8   4  108 93 3.85 2.32 18.61  1  1    4    1

To leave a comment for the author, please follow the link and comment on their blog: That’s so Random.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)