<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/quoting-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/11/quoting-in-r/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Many <a rel="nofollow" href="https://github.com/r-lib/rlang/issues/686" target="_blank"><code>R</code></a> users appear to be big fans of &quot;code capturing&quot; or <a rel="nofollow" href="http://developer.r-project.org/nonstandard-eval.pdf" target="_blank">&quot;non standard evaluation&quot;</a> (NSE) interfaces. In this note we will discuss quoting and non-quoting interfaces in <code>R</code>.</p>
<p><span></span></p>
<p>The above terms are simply talking about interfaces where a name to be used is captured from the source code the user typed, and thus does not need quote marks. For example:</p>
<div>
<pre><code>d &lt;-<span> </span><span>data.frame</span>(<span>x =</span> <span>1</span>)

d$x</code></pre>
</div>
<pre><code>## [1] 1</code></pre>
<p>Notice both during <code>data.frame</code> creation and column access: the column name is given without quotes and also accessed without quotes.</p>
<p>This differs from using a standard value oriented interface as in the following:</p>
<div>
<pre><code>d[[<span>&quot;x&quot;</span>]]</code></pre>
</div>
<pre><code>## [1] 1</code></pre>
<p>A natural reason for <code>R</code> users to look for automatic quoting is: it helps make working with columns in <code>data.frame</code>s (<code>R</code>‘s primary data analysis structure) look much like working with variables in the environment. Without the quotes a column name looks very much like a variable name. And thinking of columns as variables is a useful mindset.</p>
<p>Another place implicit quoting shows up is with <code>R</code>‘s &quot;combine&quot; operator where one can write either of the following.</p>
<div>
<pre><code><span>c</span>(<span>a =</span> <span>&quot;b&quot;</span>)</code></pre>
</div>
<pre><code>##   a 
## &quot;b&quot;</code></pre>
<div>
<pre><code><span>c</span>(<span>&quot;a&quot;</span> =<span> &quot;b&quot;</span>)</code></pre>
</div>
<pre><code>##   a 
## &quot;b&quot;</code></pre>
<p>The <a rel="nofollow" href="https://cran.r-project.org/package=wrapr" target="_blank"><code>wrapr</code></a> package brings in a new function: <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/qc.html" target="_blank"><code>qc()</code></a> or &quot;quoting <code>c()</code>&quot; that gives a very powerful and convenient way to elide quotes.</p>
<div>
<pre><code><span>library</span>(wrapr)

<span>qc</span>(<span>a =</span> b)</code></pre>
</div>
<pre><code>##   a 
## &quot;b&quot;</code></pre>
<p>Notice quotes are not required on either side of the name assignment. Again, eliding quotes is not that big a deal, and not to everyone’s taste. For example I have never seen a Python user feel they are missing anything because they write &quot;<code>{&quot;a&quot; : &quot;b&quot;}</code>&quot; to construct their own named dictionary structure.</p>
<p>That being said, <code>qc()</code> is a very convenient and consistent notation if you do want to work in an NSE style.</p>
<p>For example, if it ever bothered you that <code>dplyr</code> join takes the join column names as a character vector you can use <code>qc()</code> to instead write:</p>
<div>
<pre><code>dplyr::<span>full_join</span>(
  iris, iris, 
  <span>by =</span> <span>qc</span>(Sepal.Length, Sepal.Width, 
          Petal.Length, Petal.Width, 
          Species))</code></pre>
</div>
<p>(Actually I very much like that the join takes the columns as a vector, as it is much easier to program over.) I feel the <code>qc()</code> grouping of the columns makes it easier for a reader to see which arguments are the column set than a use of <code>...</code> would. Please take, as an example, the following <code>dplyr::group_by()</code>:</p>
<div>
<pre><code><span>library</span>(dplyr)

starwars %>%
<span> </span><span>group_by</span>(homeworld, species, <span>add =</span> <span>FALSE</span>) %>%
<span> </span><span>summarize</span>(<span>mass =</span> <span>mean</span>(mass, <span>na.rm =</span> <span>TRUE</span>))</code></pre>
</div>
<pre><code>## # A tibble: 58 x 3
## # Groups:   homeworld [?]
##    homeworld      species    mass
##    </code></pre>