<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/tidy-evaluation-most-common-actions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://edwinth.github.io/blog/dplyr-recipes/"> That’s so Random</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Tidy evaluation is a bit challenging to get your head around. Even after reading <a rel="nofollow" href="https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html" target="_blank">programming with dplyr</a> 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 <a rel="nofollow" href="https://github.com/EdwinTh/EdwinTh.github.io/tree/master/_source/2017-08-24-dplyr-recipes.Rmd" target="_blank">this file</a> or send an email.</p>
<div>
<pre><code><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span></code></pre>
</div>
<h4>bare to quosure: <code>quo</code></h4>
<div>
<pre><code><span>bare_to_quo</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>var</span><span>){</span><span>
</span><span>x</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>!!</span><span>var</span><span>)</span><span> </span><span>%>%</span><span> </span><span>head</span><span>(</span><span>1</span><span>)</span><span>
</span><span>}</span><span>
</span><span>bare_to_quo</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>quo</span><span>(</span><span>cyl</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##           cyl
## Mazda RX4   6
</code></pre>
</div>
<h4>bare to quosure in function: <code>enquo</code></h4>
<div>
<pre><code><span>bare_to_quo_in_func</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>var</span><span>)</span><span> </span><span>{</span><span>
</span><span>var_enq</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>var</span><span>)</span><span>
</span><span>x</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>!!</span><span>var_enq</span><span>)</span><span> </span><span>%>%</span><span> </span><span>head</span><span>(</span><span>1</span><span>)</span><span>
</span><span>}</span><span>
</span><span>bare_to_quo_in_func</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>mpg</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##           mpg
## Mazda RX4  21
</code></pre>
</div>
<h4>quosure to a name: <code>quo_name</code></h4>
<div>
<pre><code><span>bare_to_name</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>nm</span><span>)</span><span> </span><span>{</span><span>
</span><span>nm_name</span><span> </span><span>&lt;-</span><span> </span><span>quo_name</span><span>(</span><span>nm</span><span>)</span><span>
</span><span>x</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>!!</span><span>nm_name</span><span> </span><span>:=</span><span> </span><span>42</span><span>)</span><span> </span><span>%>%</span><span> </span><span>head</span><span>(</span><span>1</span><span>)</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>!!</span><span>nm</span><span>)</span><span>
</span><span>}</span><span>
</span><span>bare_to_name</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>quo</span><span>(</span><span>this_is_42</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##   this_is_42
## 1         42
</code></pre>
</div>
<h4>quosure to text: <code>quo_text</code></h4>
<div>
<pre><code><span>quo_to_text</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>var</span><span>)</span><span> </span><span>{</span><span>
</span><span>var_enq</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>var</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>x</span><span>,</span><span> </span><span>aes_string</span><span>(</span><span>rlang</span><span>::</span><span>quo_text</span><span>(</span><span>var_enq</span><span>)))</span><span> </span><span>+</span><span> </span><span>geom_density</span><span>()</span><span>
</span><span>}</span><span>
</span><span>plt</span><span> </span><span>&lt;-</span><span> </span><span>quo_to_text</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>cyl</span><span>)</span><span>
</span></code></pre>
</div>
<p>Note that tidy evaluation is not yet implemented in <code>ggplot2</code>, but this will be in future versions. This is a workaround for the meantime, when combining <code>dplyr</code> and <code>ggplot2</code>.</p>
<h4>character to quosure: <code>sym</code></h4>
<div>
<pre><code><span>char_to_quo</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>var</span><span>)</span><span> </span><span>{</span><span>
</span><span>var_enq</span><span> </span><span>&lt;-</span><span> </span><span>rlang</span><span>::</span><span>sym</span><span>(</span><span>var</span><span>)</span><span>
</span><span>x</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>!!</span><span>var_enq</span><span>)</span><span> </span><span>%>%</span><span> </span><span>head</span><span>(</span><span>1</span><span>)</span><span>
</span><span>}</span><span>
</span><span>char_to_quo</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>&quot;vs&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##           vs
## Mazda RX4  0
</code></pre>
</div>
<h4>multiple bares to quosure: <code>quos</code></h4>
<div>
<pre><code><span>bare_to_quo_mult</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>...</span><span>)</span><span> </span><span>{</span><span>
</span><span>grouping</span><span> </span><span>&lt;-</span><span> </span><span>quos</span><span>(</span><span>...</span><span>)</span><span>
</span><span>x</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>!!!</span><span>grouping</span><span>)</span><span> </span><span>%>%</span><span> </span><span>summarise</span><span>(</span><span>nr</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span>
</span><span>}</span><span>
</span><span>bare_to_quo_mult</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>vs</span><span>,</span><span> </span><span>cyl</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## # A tibble: 5 x 3
## # Groups:   vs [?]
##      vs   cyl    nr
##   </code></pre></div>