| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
<meta name="author" content="Hadley Wickham" /> |
| 14 |
|
| 15 |
<meta name="date" content="2026-04-03" /> |
| 16 |
|
| 17 |
<title>Non-standard evaluation</title> |
| 18 |
|
| 19 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 20 |
// be compatible with the behavior of Pandoc < 2.8). |
| 21 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 22 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 23 |
var i, h, a; |
| 24 |
for (i = 0; i < hs.length; i++) { |
| 25 |
h = hs[i]; |
| 26 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 27 |
a = h.attributes; |
| 28 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 29 |
} |
| 30 |
}); |
| 31 |
</script> |
| 32 |
|
| 33 |
<style type="text/css"> |
| 34 |
code{white-space: pre-wrap;} |
| 35 |
span.smallcaps{font-variant: small-caps;} |
| 36 |
span.underline{text-decoration: underline;} |
| 37 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 38 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 39 |
ul.task-list{list-style: none;} |
| 40 |
</style> |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
<style type="text/css"> |
| 45 |
code { |
| 46 |
white-space: pre; |
| 47 |
} |
| 48 |
.sourceCode { |
| 49 |
overflow: visible; |
| 50 |
} |
| 51 |
</style> |
| 52 |
<style type="text/css" data-origin="pandoc"> |
| 53 |
html { -webkit-text-size-adjust: 100%; } |
| 54 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 55 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 56 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 57 |
.sourceCode { overflow: visible; } |
| 58 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 59 |
div.sourceCode { margin: 1em 0; } |
| 60 |
pre.sourceCode { margin: 0; } |
| 61 |
@media screen { |
| 62 |
div.sourceCode { overflow: auto; } |
| 63 |
} |
| 64 |
@media print { |
| 65 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 66 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 67 |
} |
| 68 |
pre.numberSource code |
| 69 |
{ counter-reset: source-line 0; } |
| 70 |
pre.numberSource code > span |
| 71 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 72 |
pre.numberSource code > span > a:first-child::before |
| 73 |
{ content: counter(source-line); |
| 74 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 75 |
border: none; display: inline-block; |
| 76 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 77 |
-khtml-user-select: none; -moz-user-select: none; |
| 78 |
-ms-user-select: none; user-select: none; |
| 79 |
padding: 0 4px; width: 4em; |
| 80 |
color: #aaaaaa; |
| 81 |
} |
| 82 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 83 |
div.sourceCode |
| 84 |
{ } |
| 85 |
@media screen { |
| 86 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 87 |
} |
| 88 |
code span.al { color: #ff0000; font-weight: bold; } |
| 89 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 90 |
code span.at { color: #7d9029; } |
| 91 |
code span.bn { color: #40a070; } |
| 92 |
code span.bu { color: #008000; } |
| 93 |
code span.cf { color: #007020; font-weight: bold; } |
| 94 |
code span.ch { color: #4070a0; } |
| 95 |
code span.cn { color: #880000; } |
| 96 |
code span.co { color: #60a0b0; font-style: italic; } |
| 97 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 98 |
code span.do { color: #ba2121; font-style: italic; } |
| 99 |
code span.dt { color: #902000; } |
| 100 |
code span.dv { color: #40a070; } |
| 101 |
code span.er { color: #ff0000; font-weight: bold; } |
| 102 |
code span.ex { } |
| 103 |
code span.fl { color: #40a070; } |
| 104 |
code span.fu { color: #06287e; } |
| 105 |
code span.im { color: #008000; font-weight: bold; } |
| 106 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 107 |
code span.kw { color: #007020; font-weight: bold; } |
| 108 |
code span.op { color: #666666; } |
| 109 |
code span.ot { color: #007020; } |
| 110 |
code span.pp { color: #bc7a00; } |
| 111 |
code span.sc { color: #4070a0; } |
| 112 |
code span.ss { color: #bb6688; } |
| 113 |
code span.st { color: #4070a0; } |
| 114 |
code span.va { color: #19177c; } |
| 115 |
code span.vs { color: #4070a0; } |
| 116 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 117 |
</style> |
| 118 |
<script> |
| 119 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 120 |
(function() { |
| 121 |
var sheets = document.styleSheets; |
| 122 |
for (var i = 0; i < sheets.length; i++) { |
| 123 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 124 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 125 |
var j = 0; |
| 126 |
while (j < rules.length) { |
| 127 |
var rule = rules[j]; |
| 128 |
// check if there is a div.sourceCode rule |
| 129 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 130 |
j++; |
| 131 |
continue; |
| 132 |
} |
| 133 |
var style = rule.style.cssText; |
| 134 |
// check if color or background-color is set |
| 135 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 136 |
j++; |
| 137 |
continue; |
| 138 |
} |
| 139 |
// replace div.sourceCode by a pre.sourceCode rule |
| 140 |
sheets[i].deleteRule(j); |
| 141 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 142 |
} |
| 143 |
} |
| 144 |
})(); |
| 145 |
</script> |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
<style type="text/css">body { |
| 151 |
background-color: #fff; |
| 152 |
margin: 1em auto; |
| 153 |
max-width: 700px; |
| 154 |
overflow: visible; |
| 155 |
padding-left: 2em; |
| 156 |
padding-right: 2em; |
| 157 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 158 |
font-size: 14px; |
| 159 |
line-height: 1.35; |
| 160 |
} |
| 161 |
#TOC { |
| 162 |
clear: both; |
| 163 |
margin: 0 0 10px 10px; |
| 164 |
padding: 4px; |
| 165 |
width: 400px; |
| 166 |
border: 1px solid #CCCCCC; |
| 167 |
border-radius: 5px; |
| 168 |
background-color: #f6f6f6; |
| 169 |
font-size: 13px; |
| 170 |
line-height: 1.3; |
| 171 |
} |
| 172 |
#TOC .toctitle { |
| 173 |
font-weight: bold; |
| 174 |
font-size: 15px; |
| 175 |
margin-left: 5px; |
| 176 |
} |
| 177 |
#TOC ul { |
| 178 |
padding-left: 40px; |
| 179 |
margin-left: -1.5em; |
| 180 |
margin-top: 5px; |
| 181 |
margin-bottom: 5px; |
| 182 |
} |
| 183 |
#TOC ul ul { |
| 184 |
margin-left: -2em; |
| 185 |
} |
| 186 |
#TOC li { |
| 187 |
line-height: 16px; |
| 188 |
} |
| 189 |
table { |
| 190 |
margin: 1em auto; |
| 191 |
border-width: 1px; |
| 192 |
border-color: #DDDDDD; |
| 193 |
border-style: outset; |
| 194 |
border-collapse: collapse; |
| 195 |
} |
| 196 |
table th { |
| 197 |
border-width: 2px; |
| 198 |
padding: 5px; |
| 199 |
border-style: inset; |
| 200 |
} |
| 201 |
table td { |
| 202 |
border-width: 1px; |
| 203 |
border-style: inset; |
| 204 |
line-height: 18px; |
| 205 |
padding: 5px 5px; |
| 206 |
} |
| 207 |
table, table th, table td { |
| 208 |
border-left-style: none; |
| 209 |
border-right-style: none; |
| 210 |
} |
| 211 |
table thead, table tr.even { |
| 212 |
background-color: #f7f7f7; |
| 213 |
} |
| 214 |
p { |
| 215 |
margin: 0.5em 0; |
| 216 |
} |
| 217 |
blockquote { |
| 218 |
background-color: #f6f6f6; |
| 219 |
padding: 0.25em 0.75em; |
| 220 |
} |
| 221 |
hr { |
| 222 |
border-style: solid; |
| 223 |
border: none; |
| 224 |
border-top: 1px solid #777; |
| 225 |
margin: 28px 0; |
| 226 |
} |
| 227 |
dl { |
| 228 |
margin-left: 0; |
| 229 |
} |
| 230 |
dl dd { |
| 231 |
margin-bottom: 13px; |
| 232 |
margin-left: 13px; |
| 233 |
} |
| 234 |
dl dt { |
| 235 |
font-weight: bold; |
| 236 |
} |
| 237 |
ul { |
| 238 |
margin-top: 0; |
| 239 |
} |
| 240 |
ul li { |
| 241 |
list-style: circle outside; |
| 242 |
} |
| 243 |
ul ul { |
| 244 |
margin-bottom: 0; |
| 245 |
} |
| 246 |
pre, code { |
| 247 |
background-color: #f7f7f7; |
| 248 |
border-radius: 3px; |
| 249 |
color: #333; |
| 250 |
white-space: pre-wrap; |
| 251 |
} |
| 252 |
pre { |
| 253 |
border-radius: 3px; |
| 254 |
margin: 5px 0px 10px 0px; |
| 255 |
padding: 10px; |
| 256 |
} |
| 257 |
pre:not([class]) { |
| 258 |
background-color: #f7f7f7; |
| 259 |
} |
| 260 |
code { |
| 261 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 262 |
font-size: 85%; |
| 263 |
} |
| 264 |
p > code, li > code { |
| 265 |
padding: 2px 0px; |
| 266 |
} |
| 267 |
div.figure { |
| 268 |
text-align: center; |
| 269 |
} |
| 270 |
img { |
| 271 |
background-color: #FFFFFF; |
| 272 |
padding: 2px; |
| 273 |
border: 1px solid #DDDDDD; |
| 274 |
border-radius: 3px; |
| 275 |
border: 1px solid #CCCCCC; |
| 276 |
margin: 0 5px; |
| 277 |
} |
| 278 |
h1 { |
| 279 |
margin-top: 0; |
| 280 |
font-size: 35px; |
| 281 |
line-height: 40px; |
| 282 |
} |
| 283 |
h2 { |
| 284 |
border-bottom: 4px solid #f7f7f7; |
| 285 |
padding-top: 10px; |
| 286 |
padding-bottom: 2px; |
| 287 |
font-size: 145%; |
| 288 |
} |
| 289 |
h3 { |
| 290 |
border-bottom: 2px solid #f7f7f7; |
| 291 |
padding-top: 10px; |
| 292 |
font-size: 120%; |
| 293 |
} |
| 294 |
h4 { |
| 295 |
border-bottom: 1px solid #f7f7f7; |
| 296 |
margin-left: 8px; |
| 297 |
font-size: 105%; |
| 298 |
} |
| 299 |
h5, h6 { |
| 300 |
border-bottom: 1px solid #ccc; |
| 301 |
font-size: 105%; |
| 302 |
} |
| 303 |
a { |
| 304 |
color: #0033dd; |
| 305 |
text-decoration: none; |
| 306 |
} |
| 307 |
a:hover { |
| 308 |
color: #6666ff; } |
| 309 |
a:visited { |
| 310 |
color: #800080; } |
| 311 |
a:visited:hover { |
| 312 |
color: #BB00BB; } |
| 313 |
a[href^="http:"] { |
| 314 |
text-decoration: underline; } |
| 315 |
a[href^="https:"] { |
| 316 |
text-decoration: underline; } |
| 317 |
|
| 318 |
code > span.kw { color: #555; font-weight: bold; } |
| 319 |
code > span.dt { color: #902000; } |
| 320 |
code > span.dv { color: #40a070; } |
| 321 |
code > span.bn { color: #d14; } |
| 322 |
code > span.fl { color: #d14; } |
| 323 |
code > span.ch { color: #d14; } |
| 324 |
code > span.st { color: #d14; } |
| 325 |
code > span.co { color: #888888; font-style: italic; } |
| 326 |
code > span.ot { color: #007020; } |
| 327 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 328 |
code > span.fu { color: #900; font-weight: bold; } |
| 329 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 330 |
</style> |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
</head> |
| 336 |
|
| 337 |
<body> |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
<h1 class="title toc-ignore">Non-standard evaluation</h1> |
| 343 |
<h4 class="author">Hadley Wickham</h4> |
| 344 |
<h4 class="date">2026-04-03</h4> |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
<p>This document describes lazyeval, a package that provides principled |
| 349 |
tools to perform non-standard evaluation (NSE) in R. You should read |
| 350 |
this vignette if you want to program with packages like dplyr and |
| 351 |
ggplot2<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>, |
| 352 |
or you want a principled way of working with delayed expressions in your |
| 353 |
own package. As the name suggests, non-standard evaluation breaks away |
| 354 |
from the standard evaluation (SE) rules in order to do something |
| 355 |
special. There are three common uses of NSE:</p> |
| 356 |
<ol style="list-style-type: decimal"> |
| 357 |
<li><p><strong>Labelling</strong> enhances plots and tables by using the |
| 358 |
expressions supplied to a function, rather than their values. For |
| 359 |
example, note the axis labels in this plot:</p> |
| 360 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mar =</span> <span class="fu">c</span>(<span class="fl">4.5</span>, <span class="fl">4.5</span>, <span class="dv">1</span>, <span class="fl">0.5</span>))</span> |
| 361 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a>grid <span class="ot"><-</span> <span class="fu">seq</span>(<span class="dv">0</span>, <span class="dv">2</span> <span class="sc">*</span> pi, <span class="at">length =</span> <span class="dv">100</span>)</span> |
| 362 |
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="fu">plot</span>(grid, <span class="fu">sin</span>(grid), <span class="at">type =</span> <span class="st">"l"</span>)</span></code></pre></div> |
| 363 |
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAADwCAYAAAAJkrPKAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1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+ginwIIIZyFuRbN++XS5evBhMNTyWBGIJUAHEouAHEogMgSJFiih3z65du6oJ3nnz5kmtWrUEk7lQDIGWVq1ayZ133imdO3eWU6dOyfLly6Vt27aBns7jSCAeASqAeEi4gQSMJwAPnnbt2sm0adPkhRdeUOsBcEFPmzZtwI3B/j9//nw1FwC7PyaVGzZsKLlz5w64Dh5IAt4EkuluaLf90Lz38DMJkIAhBGCywUQwTDiYtPWUpk2bKu+ghQsXejYF/I41AHgagDcRCwmESoBPAKGS43kkECAB2PurVq0qgwYNUhd8nLZkyRLBhb9x48YB1uJ7GJ4GePH3ZcJvwRPgE0DwzHgGCQRN4N1335Vhw4bJ9evXJU2aNHLixAkVDgIrgllIIFoEqACiRZ7tuo4AQjZs3LhRzpw5I/AMypgxo+sYsMPWIkAFYK3xoDQkQAIkYBoBzgGYhpoNkQAJkIC1CFABWGs8KA0JkAAJmEaACsA01GyIBEiABKxFgArAWuNBaUiABEjANAJUAKahZkMkQAIkYC0CVADWGg9KQwIkQAKmEaACMA01GyIBEiABaxGgArDWeFAaEiABEjCNwP8De3Qu7oXuuPcAAAAASUVORK5CYII=" /></p></li> |
| 364 |
<li><p><strong>Non-standard scoping</strong> looks for objects in places |
| 365 |
other than the current environment. For example, base R has |
| 366 |
<code>with()</code>, <code>subset()</code>, and <code>transform()</code> |
| 367 |
that look for objects in a data frame (or list) before the current |
| 368 |
environment:</p> |
| 369 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">4</span>, <span class="dv">2</span>, <span class="dv">3</span>), <span class="at">y =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">4</span>, <span class="dv">3</span>))</span> |
| 370 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a></span> |
| 371 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="fu">with</span>(df, <span class="fu">mean</span>(x))</span> |
| 372 |
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co">#> [1] 3</span></span> |
| 373 |
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="fu">subset</span>(df, x <span class="sc">==</span> y)</span> |
| 374 |
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 375 |
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co">#> 5 3 3</span></span> |
| 376 |
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="fu">transform</span>(df, <span class="at">z =</span> x <span class="sc">+</span> y)</span> |
| 377 |
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co">#> x y z</span></span> |
| 378 |
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="co">#> 1 1 2 3</span></span> |
| 379 |
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a><span class="co">#> 2 5 1 6</span></span> |
| 380 |
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a><span class="co">#> 3 4 5 9</span></span> |
| 381 |
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a><span class="co">#> 4 2 4 6</span></span> |
| 382 |
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a><span class="co">#> 5 3 3 6</span></span></code></pre></div></li> |
| 383 |
<li><p><strong>Metaprogramming</strong> is a catch-all term that covers |
| 384 |
all other uses of NSE (such as in <code>bquote()</code> and |
| 385 |
<code>library()</code>). Metaprogramming is so called because it |
| 386 |
involves computing on the unevaluated code in some way.</p></li> |
| 387 |
</ol> |
| 388 |
<p>This document is broadly organised according to the three types of |
| 389 |
non-standard evaluation described above. The main difference is that |
| 390 |
after <a href="#labelling">labelling</a>, we’ll take a detour to learn |
| 391 |
more about <a href="#formulas">formulas</a>. You’re probably familiar |
| 392 |
with formulas from linear models |
| 393 |
(e.g. <code>lm(mpg ~ displ, data = mtcars)</code>) but formulas are more |
| 394 |
than just a tool for modelling: they are a general way of capturing an |
| 395 |
unevaluated expression.</p> |
| 396 |
<p>The approaches recommended here are quite different to my previous |
| 397 |
generation of recommendations. I am fairly confident these new |
| 398 |
approaches are correct, and will not have to change substantially again. |
| 399 |
The current tools make it easy to solve a number of practical problems |
| 400 |
that were previously challenging and are rooted in long-standing |
| 401 |
theory.</p> |
| 402 |
<div id="labelling" class="section level2"> |
| 403 |
<h2>Labelling</h2> |
| 404 |
<p>In base R, the classic way to turn an argument into a label is to use |
| 405 |
<code>deparse(substitute(x))</code>:</p> |
| 406 |
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>my_label <span class="ot"><-</span> <span class="cf">function</span>(x) <span class="fu">deparse</span>(<span class="fu">substitute</span>(x))</span> |
| 407 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="fu">my_label</span>(x <span class="sc">+</span> y)</span> |
| 408 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#> [1] "x + y"</span></span></code></pre></div> |
| 409 |
<p>There are two potential problems with this approach:</p> |
| 410 |
<ol style="list-style-type: decimal"> |
| 411 |
<li><p>For long some expressions, <code>deparse()</code> generates a |
| 412 |
character vector with length > 1:</p> |
| 413 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="fu">my_label</span>({</span> |
| 414 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a> a <span class="sc">+</span> b</span> |
| 415 |
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a> c <span class="sc">+</span> d</span> |
| 416 |
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a>})</span> |
| 417 |
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#> [1] "{" " a + b" " c + d" "}"</span></span></code></pre></div></li> |
| 418 |
<li><p><code>substitute()</code> only looks one level up, so you lose |
| 419 |
the original label if the function isn’t called directly:</p> |
| 420 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>my_label2 <span class="ot"><-</span> <span class="cf">function</span>(x) <span class="fu">my_label</span>(x)</span> |
| 421 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="fu">my_label2</span>(a <span class="sc">+</span> b)</span> |
| 422 |
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="co">#> [1] "x"</span></span></code></pre></div></li> |
| 423 |
</ol> |
| 424 |
<p>Both of these problems are resolved by |
| 425 |
<code>lazyeval::expr_text()</code>:</p> |
| 426 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>my_label <span class="ot"><-</span> <span class="cf">function</span>(x) <span class="fu">expr_text</span>(x)</span> |
| 427 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a>my_label2 <span class="ot"><-</span> <span class="cf">function</span>(x) <span class="fu">my_label</span>(x)</span> |
| 428 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a> </span> |
| 429 |
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="fu">my_label</span>({</span> |
| 430 |
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a> a <span class="sc">+</span> b</span> |
| 431 |
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a> c <span class="sc">+</span> d</span> |
| 432 |
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a>})</span> |
| 433 |
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#> [1] "{\n a + b\n c + d\n}"</span></span> |
| 434 |
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="fu">my_label2</span>(a <span class="sc">+</span> b)</span> |
| 435 |
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#> [1] "a + b"</span></span></code></pre></div> |
| 436 |
<p>There are two variations on the theme of |
| 437 |
<code>expr_text()</code>:</p> |
| 438 |
<ul> |
| 439 |
<li><p><code>expr_find()</code> find the underlying expression. It works |
| 440 |
similarly to <code>substitute()</code> but will follow a chain of |
| 441 |
promises back up to the original expression. This is often useful for <a href="#metaprogramming">metaprogramming</a>.</p></li> |
| 442 |
<li><p><code>expr_label()</code> is a customised version of |
| 443 |
<code>expr_text()</code> that produces labels designed to be used in |
| 444 |
messages to the user:</p> |
| 445 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="fu">expr_label</span>(x)</span> |
| 446 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co">#> [1] "`x`"</span></span> |
| 447 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="fu">expr_label</span>(a <span class="sc">+</span> b <span class="sc">+</span> c)</span> |
| 448 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co">#> [1] "`a + b + c`"</span></span> |
| 449 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="fu">expr_label</span>(<span class="fu">foo</span>({</span> |
| 450 |
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a> x <span class="sc">+</span> y</span> |
| 451 |
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a>}))</span> |
| 452 |
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co">#> [1] "`foo(...)`"</span></span></code></pre></div></li> |
| 453 |
</ul> |
| 454 |
<div id="exercises" class="section level3"> |
| 455 |
<h3>Exercises</h3> |
| 456 |
<ol style="list-style-type: decimal"> |
| 457 |
<li><p><code>plot()</code> uses <code>deparse(substitute(x))</code> to |
| 458 |
generate labels for the x and y axes. Can you generate input that causes |
| 459 |
it to display bad labels? Write your own wrapper around |
| 460 |
<code>plot()</code> that uses <code>expr_label()</code> to compute |
| 461 |
<code>xlim</code> and <code>ylim</code>.</p></li> |
| 462 |
<li><p>Create a simple implementation of <code>mean()</code> that stops |
| 463 |
with an informative error message if the argument is not numeric:</p> |
| 464 |
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>)</span> |
| 465 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="fu">my_mean</span>(x)</span> |
| 466 |
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="co">#> Error: `x` is a not a numeric vector.</span></span> |
| 467 |
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="fu">my_mean</span>(x <span class="sc">==</span> <span class="st">"a"</span>)</span> |
| 468 |
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co">#> Error: `x == "a"` is not a numeric vector.</span></span> |
| 469 |
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="fu">my_mean</span>(<span class="st">"a"</span>)</span> |
| 470 |
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co">#> Error: "a" is not a numeric vector.</span></span></code></pre></div></li> |
| 471 |
<li><p>Read the source code for <code>expr_text()</code>. How does it |
| 472 |
work? What additional arguments to <code>deparse()</code> does it |
| 473 |
use?</p></li> |
| 474 |
</ol> |
| 475 |
</div> |
| 476 |
</div> |
| 477 |
<div id="formulas" class="section level2"> |
| 478 |
<h2>Formulas</h2> |
| 479 |
<p>Non-standard scoping is probably the most useful NSE tool, but before |
| 480 |
we can talk about a solid approach, we need to take a detour to talk |
| 481 |
about formulas. Formulas are a familiar tool from linear models, but |
| 482 |
their utility is not limited to models. In fact, formulas are a |
| 483 |
powerful, general purpose tool, because a formula captures two |
| 484 |
things:</p> |
| 485 |
<ol style="list-style-type: decimal"> |
| 486 |
<li>An unevaluated expression.</li> |
| 487 |
<li>The context (environment) in which the expression was created.</li> |
| 488 |
</ol> |
| 489 |
<p><code>~</code> is a single character that allows you to say: “I want |
| 490 |
to capture the meaning of this code, without evaluating it right away”. |
| 491 |
For that reason, the formula can be thought of as a “quoting” |
| 492 |
operator.</p> |
| 493 |
<div id="definition-of-a-formula" class="section level3"> |
| 494 |
<h3>Definition of a formula</h3> |
| 495 |
<p>Technically, a formula is a “language” object (i.e. an unevaluated |
| 496 |
expression) with a class of “formula” and an attribute that stores the |
| 497 |
environment:</p> |
| 498 |
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>f <span class="ot"><-</span> <span class="er">~</span> x <span class="sc">+</span> y <span class="sc">+</span> z</span> |
| 499 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a><span class="fu">typeof</span>(f)</span> |
| 500 |
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="co">#> [1] "language"</span></span> |
| 501 |
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="fu">attributes</span>(f)</span> |
| 502 |
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#> $class</span></span> |
| 503 |
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co">#> [1] "formula"</span></span> |
| 504 |
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="co">#> </span></span> |
| 505 |
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#> $.Environment</span></span> |
| 506 |
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co">#> <environment: R_GlobalEnv></span></span></code></pre></div> |
| 507 |
<p>The structure of the underlying object is slightly different |
| 508 |
depending on whether you have a one-sided or two-sided formula:</p> |
| 509 |
<ul> |
| 510 |
<li><p>One-sided formulas have length two:</p> |
| 511 |
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="fu">length</span>(f)</span> |
| 512 |
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="co">#> [1] 2</span></span> |
| 513 |
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="co"># The 1st element is always ~</span></span> |
| 514 |
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a>f[[<span class="dv">1</span>]]</span> |
| 515 |
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#> `~`</span></span> |
| 516 |
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co"># The 2nd element is the RHS</span></span> |
| 517 |
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a>f[[<span class="dv">2</span>]]</span> |
| 518 |
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co">#> x + y + z</span></span></code></pre></div></li> |
| 519 |
<li><p>Two-sided formulas have length three:</p> |
| 520 |
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>g <span class="ot"><-</span> y <span class="sc">~</span> x <span class="sc">+</span> z</span> |
| 521 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="fu">length</span>(g)</span> |
| 522 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co">#> [1] 3</span></span> |
| 523 |
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="co"># The 1st element is still ~</span></span> |
| 524 |
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a>g[[<span class="dv">1</span>]]</span> |
| 525 |
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a><span class="co">#> `~`</span></span> |
| 526 |
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co"># But now the 2nd element is the LHS</span></span> |
| 527 |
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a>g[[<span class="dv">2</span>]]</span> |
| 528 |
<span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="co">#> y</span></span> |
| 529 |
<span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="co"># And the 3rd element is the RHS</span></span> |
| 530 |
<span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a>g[[<span class="dv">3</span>]]</span> |
| 531 |
<span id="cb11-12"><a href="#cb11-12" tabindex="-1"></a><span class="co">#> x + z</span></span></code></pre></div></li> |
| 532 |
</ul> |
| 533 |
<p>To abstract away these differences, lazyeval provides |
| 534 |
<code>f_rhs()</code> and <code>f_lhs()</code> to access either side of |
| 535 |
the formula, and <code>f_env()</code> to access its environment:</p> |
| 536 |
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="fu">f_rhs</span>(f)</span> |
| 537 |
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="co">#> x + y + z</span></span> |
| 538 |
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="fu">f_lhs</span>(f)</span> |
| 539 |
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="co">#> NULL</span></span> |
| 540 |
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="fu">f_env</span>(f)</span> |
| 541 |
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#> <environment: R_GlobalEnv></span></span> |
| 542 |
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a></span> |
| 543 |
<span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="fu">f_rhs</span>(g)</span> |
| 544 |
<span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a><span class="co">#> x + z</span></span> |
| 545 |
<span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="fu">f_lhs</span>(g)</span> |
| 546 |
<span id="cb12-11"><a href="#cb12-11" tabindex="-1"></a><span class="co">#> y</span></span> |
| 547 |
<span id="cb12-12"><a href="#cb12-12" tabindex="-1"></a><span class="fu">f_env</span>(g)</span> |
| 548 |
<span id="cb12-13"><a href="#cb12-13" tabindex="-1"></a><span class="co">#> <environment: R_GlobalEnv></span></span></code></pre></div> |
| 549 |
</div> |
| 550 |
<div id="evaluating-a-formula" class="section level3"> |
| 551 |
<h3>Evaluating a formula</h3> |
| 552 |
<p>A formula captures delays the evaluation of an expression so you can |
| 553 |
later evaluate it with <code>f_eval()</code>:</p> |
| 554 |
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>f <span class="ot"><-</span> <span class="er">~</span> <span class="dv">1</span> <span class="sc">+</span> <span class="dv">2</span> <span class="sc">+</span> <span class="dv">3</span></span> |
| 555 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a>f</span> |
| 556 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="co">#> ~1 + 2 + 3</span></span> |
| 557 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="fu">f_eval</span>(f)</span> |
| 558 |
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#> [1] 6</span></span></code></pre></div> |
| 559 |
<p>This allows you to use a formula as a robust way of delaying |
| 560 |
evaluation, cleanly separating the creation of the formula from its |
| 561 |
evaluation. Because formulas capture the code and context, you get the |
| 562 |
correct result even when a formula is created and evaluated in different |
| 563 |
places. In the following example, note that the value of <code>x</code> |
| 564 |
inside <code>add_1000()</code> is used:</p> |
| 565 |
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="dv">1</span></span> |
| 566 |
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a>add_1000 <span class="ot"><-</span> <span class="cf">function</span>(x) {</span> |
| 567 |
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a> <span class="sc">~</span> <span class="dv">1000</span> <span class="sc">+</span> x</span> |
| 568 |
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a>}</span> |
| 569 |
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a></span> |
| 570 |
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="fu">add_1000</span>(<span class="dv">3</span>)</span> |
| 571 |
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#> ~1000 + x</span></span> |
| 572 |
<span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="co">#> <environment: 0x8b0e70a50></span></span> |
| 573 |
<span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="fu">add_1000</span>(<span class="dv">3</span>))</span> |
| 574 |
<span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="co">#> [1] 1003</span></span></code></pre></div> |
| 575 |
<p>It can be hard to see what’s going on when looking at a formula |
| 576 |
because important values are stored in the environment, which is largely |
| 577 |
opaque. You can use <code>f_unwrap()</code> to replace names with their |
| 578 |
corresponding values:</p> |
| 579 |
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="fu">f_unwrap</span>(<span class="fu">add_1000</span>(<span class="dv">3</span>))</span> |
| 580 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="co">#> ~1000 + 3</span></span></code></pre></div> |
| 581 |
</div> |
| 582 |
<div id="non-standard-scoping" class="section level3"> |
| 583 |
<h3>Non-standard scoping</h3> |
| 584 |
<p><code>f_eval()</code> has an optional second argument: a named list |
| 585 |
(or data frame) that overrides values found in the formula’s |
| 586 |
environment.</p> |
| 587 |
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>y <span class="ot"><-</span> <span class="dv">100</span></span> |
| 588 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> y)</span> |
| 589 |
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a><span class="co">#> [1] 100</span></span> |
| 590 |
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> y, <span class="at">data =</span> <span class="fu">list</span>(<span class="at">y =</span> <span class="dv">10</span>))</span> |
| 591 |
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="co">#> [1] 10</span></span> |
| 592 |
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a></span> |
| 593 |
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a><span class="co"># Can mix variables in environment and data argument</span></span> |
| 594 |
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> x <span class="sc">+</span> y, <span class="at">data =</span> <span class="fu">list</span>(<span class="at">x =</span> <span class="dv">10</span>))</span> |
| 595 |
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a><span class="co">#> [1] 110</span></span> |
| 596 |
<span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a><span class="co"># Can even supply functions</span></span> |
| 597 |
<span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> <span class="fu">f</span>(y), <span class="at">data =</span> <span class="fu">list</span>(<span class="at">f =</span> <span class="cf">function</span>(x) x <span class="sc">*</span> <span class="dv">3</span>))</span> |
| 598 |
<span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a><span class="co">#> [1] 300</span></span></code></pre></div> |
| 599 |
<p>This makes it very easy to implement non-standard scoping:</p> |
| 600 |
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> <span class="fu">mean</span>(cyl), <span class="at">data =</span> mtcars)</span> |
| 601 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="co">#> [1] 6.1875</span></span></code></pre></div> |
| 602 |
<p>One challenge with non-standard scoping is that we’ve introduced some |
| 603 |
ambiguity. For example, in the code below does <code>x</code> come from |
| 604 |
<code>mydata</code> or the environment?</p> |
| 605 |
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> x, <span class="at">data =</span> mydata)</span></code></pre></div> |
| 606 |
<p>You can’t tell without knowing whether or not <code>mydata</code> has |
| 607 |
a variable called <code>x</code>. To overcome this problem, |
| 608 |
<code>f_eval()</code> provides two pronouns:</p> |
| 609 |
<ul> |
| 610 |
<li><code>.data</code> is bound to the data frame.</li> |
| 611 |
<li><code>.env</code> is bound to the formula environment.</li> |
| 612 |
</ul> |
| 613 |
<p>They both start with <code>.</code> to minimise the chances of |
| 614 |
clashing with existing variables.</p> |
| 615 |
<p>With these pronouns we can rewrite the previous formula to remove the |
| 616 |
ambiguity:</p> |
| 617 |
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>mydata <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="dv">100</span>, <span class="at">y =</span> <span class="dv">1</span>)</span> |
| 618 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a>x <span class="ot"><-</span> <span class="dv">10</span></span> |
| 619 |
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a></span> |
| 620 |
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> .env<span class="sc">$</span>x, <span class="at">data =</span> mydata)</span> |
| 621 |
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="co">#> [1] 10</span></span> |
| 622 |
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> .data<span class="sc">$</span>x, <span class="at">data =</span> mydata)</span> |
| 623 |
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a><span class="co">#> [1] 100</span></span></code></pre></div> |
| 624 |
<p>If the variable or object doesn’t exist, you’ll get an informative |
| 625 |
error:</p> |
| 626 |
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> .env<span class="sc">$</span>z, <span class="at">data =</span> mydata)</span> |
| 627 |
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 628 |
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="co">#> ! Object 'z' not found in environment</span></span> |
| 629 |
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> .data<span class="sc">$</span>z, <span class="at">data =</span> mydata)</span> |
| 630 |
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 631 |
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a><span class="co">#> ! Variable 'z' not found in data</span></span></code></pre></div> |
| 632 |
</div> |
| 633 |
<div id="unquoting" class="section level3"> |
| 634 |
<h3>Unquoting</h3> |
| 635 |
<p><code>f_eval()</code> has one more useful trick up its sleeve: |
| 636 |
unquoting. Unquoting allows you to write functions where the user |
| 637 |
supplies part of the formula. For example, the following function allows |
| 638 |
you to compute the mean of any column (or any function of a column):</p> |
| 639 |
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>df_mean <span class="ot"><-</span> <span class="cf">function</span>(df, variable) {</span> |
| 640 |
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a> <span class="fu">f_eval</span>(<span class="sc">~</span> <span class="fu">mean</span>(<span class="fu">uq</span>(variable)), <span class="at">data =</span> df)</span> |
| 641 |
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a>}</span> |
| 642 |
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a></span> |
| 643 |
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a><span class="fu">df_mean</span>(mtcars, <span class="sc">~</span> cyl)</span> |
| 644 |
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a><span class="co">#> [1] 6.1875</span></span> |
| 645 |
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="fu">df_mean</span>(mtcars, <span class="sc">~</span> disp <span class="sc">*</span> <span class="fl">0.01638</span>)</span> |
| 646 |
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="co">#> [1] 3.779224</span></span> |
| 647 |
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="fu">df_mean</span>(mtcars, <span class="sc">~</span> <span class="fu">sqrt</span>(mpg))</span> |
| 648 |
<span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a><span class="co">#> [1] 4.43477</span></span></code></pre></div> |
| 649 |
<p>To see how this works, we can use <code>f_interp()</code> which |
| 650 |
<code>f_eval()</code> calls internally (you shouldn’t call it in your |
| 651 |
own code, but it’s useful for debugging). The key is <code>uq()</code>: |
| 652 |
<code>uq()</code> evaluates its first (and only) argument and inserts |
| 653 |
the value into the formula:</p> |
| 654 |
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a>variable <span class="ot"><-</span> <span class="er">~</span>cyl</span> |
| 655 |
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a><span class="fu">f_interp</span>(<span class="sc">~</span> <span class="fu">mean</span>(<span class="fu">uq</span>(variable)))</span> |
| 656 |
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a><span class="co">#> ~mean(cyl)</span></span> |
| 657 |
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a></span> |
| 658 |
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a>variable <span class="ot"><-</span> <span class="er">~</span> disp <span class="sc">*</span> <span class="fl">0.01638</span></span> |
| 659 |
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a><span class="fu">f_interp</span>(<span class="sc">~</span> <span class="fu">mean</span>(<span class="fu">uq</span>(variable)))</span> |
| 660 |
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a><span class="co">#> ~mean(disp * 0.01638)</span></span></code></pre></div> |
| 661 |
<p>Unquoting allows you to create code “templates”, where you write most |
| 662 |
of the expression, while still allowing the user to control important |
| 663 |
components. You can even use <code>uq()</code> to change the function |
| 664 |
being called:</p> |
| 665 |
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a>f <span class="ot"><-</span> <span class="er">~</span> mean</span> |
| 666 |
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="fu">f_interp</span>(<span class="sc">~</span> <span class="fu">uq</span>(f)(<span class="fu">uq</span>(variable)))</span> |
| 667 |
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a><span class="co">#> ~mean(disp * 0.01638)</span></span></code></pre></div> |
| 668 |
<p>Note that <code>uq()</code> only takes the RHS of a formula, which |
| 669 |
makes it difficult to insert literal formulas into a call:</p> |
| 670 |
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a>formula <span class="ot"><-</span> y <span class="sc">~</span> x</span> |
| 671 |
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="fu">f_interp</span>(<span class="sc">~</span> <span class="fu">lm</span>(<span class="fu">uq</span>(formula), <span class="at">data =</span> df))</span> |
| 672 |
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a><span class="co">#> ~lm(x, data = df)</span></span></code></pre></div> |
| 673 |
<p>You can instead use <code>uqf()</code> which uses the whole formula, |
| 674 |
not just the RHS:</p> |
| 675 |
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a><span class="fu">f_interp</span>(<span class="sc">~</span> <span class="fu">lm</span>(<span class="fu">uqf</span>(formula), <span class="at">data =</span> df))</span> |
| 676 |
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a><span class="co">#> ~lm(y ~ x, data = df)</span></span></code></pre></div> |
| 677 |
<p>Unquoting is powerful, but it only allows you to modify a single |
| 678 |
argument: it doesn’t allow you to add an arbitrary number of arguments. |
| 679 |
To do that, you’ll need “unquote-splice”, or <code>uqs()</code>. The |
| 680 |
first (and only) argument to <code>uqs()</code> should be a list of |
| 681 |
arguments to be spliced into the call:</p> |
| 682 |
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a>variable <span class="ot"><-</span> <span class="er">~</span> x</span> |
| 683 |
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a>extra_args <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">na.rm =</span> <span class="cn">TRUE</span>, <span class="at">trim =</span> <span class="fl">0.9</span>)</span> |
| 684 |
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a><span class="fu">f_interp</span>(<span class="sc">~</span> <span class="fu">mean</span>(<span class="fu">uq</span>(variable), <span class="fu">uqs</span>(extra_args)))</span> |
| 685 |
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a><span class="co">#> ~mean(x, na.rm = TRUE, trim = 0.9)</span></span></code></pre></div> |
| 686 |
</div> |
| 687 |
<div id="exercises-1" class="section level3"> |
| 688 |
<h3>Exercises</h3> |
| 689 |
<ol style="list-style-type: decimal"> |
| 690 |
<li><p>Create a wrapper around <code>lm()</code> that allows the user to |
| 691 |
supply the response and predictors as two separate formulas.</p></li> |
| 692 |
<li><p>Compare and contrast <code>f_eval()</code> with |
| 693 |
<code>with()</code>.</p></li> |
| 694 |
<li><p>Why does this code work even though <code>f</code> is defined in |
| 695 |
two places? (And one of them is not a function).</p> |
| 696 |
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a>f <span class="ot"><-</span> <span class="cf">function</span>(x) x <span class="sc">+</span> <span class="dv">1</span></span> |
| 697 |
<span id="cb27-2"><a href="#cb27-2" tabindex="-1"></a><span class="fu">f_eval</span>(<span class="sc">~</span> <span class="fu">f</span>(<span class="dv">10</span>), <span class="fu">list</span>(<span class="at">f =</span> <span class="st">"a"</span>))</span> |
| 698 |
<span id="cb27-3"><a href="#cb27-3" tabindex="-1"></a><span class="co">#> [1] 11</span></span></code></pre></div></li> |
| 699 |
</ol> |
| 700 |
</div> |
| 701 |
</div> |
| 702 |
<div id="non-standard-scoping-1" class="section level2"> |
| 703 |
<h2>Non-standard scoping</h2> |
| 704 |
<p>Non-standard scoping (NSS) is an important part of R because it makes |
| 705 |
it easy to write functions tailored for interactive data exploration. |
| 706 |
These functions require less typing, at the cost of some ambiguity and |
| 707 |
“magic”. This is a good trade-off for interactive data exploration |
| 708 |
because you want to get ideas out of your head and into the computer as |
| 709 |
quickly as possible. If a function does make a bad guess, you’ll spot it |
| 710 |
quickly because you’re working interactively.</p> |
| 711 |
<p>There are three challenges to implementing non-standard scoping:</p> |
| 712 |
<ol style="list-style-type: decimal"> |
| 713 |
<li><p>You must correctly delay the evaluation of a function argument, |
| 714 |
capturing both the computation (the expression), and the context (the |
| 715 |
environment). I recommend making this explicit by requiring the user to |
| 716 |
“quote” any NSS arguments with <code>~</code>, and then evaluating |
| 717 |
explicit with <code>f_eval()</code>.</p></li> |
| 718 |
<li><p>When writing functions that use NSS-functions, you need some way |
| 719 |
to avoid the automatic lookup and be explicit about where objects should |
| 720 |
be found. <code>f_eval()</code> solves this problem with the |
| 721 |
<code>.data.</code> and <code>.env</code> pronouns.</p></li> |
| 722 |
<li><p>You need some way to allow the user to supply parts of a formula. |
| 723 |
<code>f_eval()</code> solves this with unquoting.</p></li> |
| 724 |
</ol> |
| 725 |
<p>To illustrate these challenges, I will implement a |
| 726 |
<code>sieve()</code> function that works similarly to |
| 727 |
<code>base::subset()</code> or <code>dplyr::filter()</code>. The goal of |
| 728 |
<code>sieve()</code> is to make it easy to select observations that |
| 729 |
match criteria defined by a logical expression. <code>sieve()</code> has |
| 730 |
three advantages over <code>[</code>:</p> |
| 731 |
<ol style="list-style-type: decimal"> |
| 732 |
<li><p>It is much more compact when the condition uses many variables, |
| 733 |
because you don’t need to repeat the name of the data frame many |
| 734 |
times.</p></li> |
| 735 |
<li><p>It drops rows where the condition evaluates to <code>NA</code>, |
| 736 |
rather than filling them with <code>NA</code>s.</p></li> |
| 737 |
<li><p>It always returns a data frame.</p></li> |
| 738 |
</ol> |
| 739 |
<p>The implementation of <code>sieve()</code> is straightforward. First |
| 740 |
we use <code>f_eval()</code> to perform NSS. Then we then check that we |
| 741 |
have a logical vector, replace <code>NA</code>s with <code>FALSE</code>, |
| 742 |
and subset with <code>[</code>.</p> |
| 743 |
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a>sieve <span class="ot"><-</span> <span class="cf">function</span>(df, condition) {</span> |
| 744 |
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a> rows <span class="ot"><-</span> <span class="fu">f_eval</span>(condition, df)</span> |
| 745 |
<span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">is.logical</span>(rows)) {</span> |
| 746 |
<span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"`condition` must be logical."</span>, <span class="at">call. =</span> <span class="cn">FALSE</span>)</span> |
| 747 |
<span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a> }</span> |
| 748 |
<span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a> </span> |
| 749 |
<span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a> rows[<span class="fu">is.na</span>(rows)] <span class="ot"><-</span> <span class="cn">FALSE</span></span> |
| 750 |
<span id="cb28-8"><a href="#cb28-8" tabindex="-1"></a> df[rows, , drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span> |
| 751 |
<span id="cb28-9"><a href="#cb28-9" tabindex="-1"></a>}</span> |
| 752 |
<span id="cb28-10"><a href="#cb28-10" tabindex="-1"></a></span> |
| 753 |
<span id="cb28-11"><a href="#cb28-11" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">y =</span> <span class="dv">5</span><span class="sc">:</span><span class="dv">1</span>)</span> |
| 754 |
<span id="cb28-12"><a href="#cb28-12" tabindex="-1"></a><span class="fu">sieve</span>(df, <span class="sc">~</span> x <span class="sc"><=</span> <span class="dv">2</span>)</span> |
| 755 |
<span id="cb28-13"><a href="#cb28-13" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 756 |
<span id="cb28-14"><a href="#cb28-14" tabindex="-1"></a><span class="co">#> 1 1 5</span></span> |
| 757 |
<span id="cb28-15"><a href="#cb28-15" tabindex="-1"></a><span class="co">#> 2 2 4</span></span> |
| 758 |
<span id="cb28-16"><a href="#cb28-16" tabindex="-1"></a><span class="fu">sieve</span>(df, <span class="sc">~</span> x <span class="sc">==</span> y)</span> |
| 759 |
<span id="cb28-17"><a href="#cb28-17" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 760 |
<span id="cb28-18"><a href="#cb28-18" tabindex="-1"></a><span class="co">#> 3 3 3</span></span></code></pre></div> |
| 761 |
<div id="programming-with-sieve" class="section level3"> |
| 762 |
<h3>Programming with <code>sieve()</code></h3> |
| 763 |
<p>Imagine that you’ve written some code that looks like this:</p> |
| 764 |
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" tabindex="-1"></a><span class="fu">sieve</span>(march, <span class="sc">~</span> x <span class="sc">></span> <span class="dv">100</span>)</span> |
| 765 |
<span id="cb29-2"><a href="#cb29-2" tabindex="-1"></a><span class="fu">sieve</span>(april, <span class="sc">~</span> x <span class="sc">></span> <span class="dv">50</span>)</span> |
| 766 |
<span id="cb29-3"><a href="#cb29-3" tabindex="-1"></a><span class="fu">sieve</span>(june, <span class="sc">~</span> x <span class="sc">></span> <span class="dv">45</span>)</span> |
| 767 |
<span id="cb29-4"><a href="#cb29-4" tabindex="-1"></a><span class="fu">sieve</span>(july, <span class="sc">~</span> x <span class="sc">></span> <span class="dv">17</span>)</span></code></pre></div> |
| 768 |
<p>(This is a contrived example, but it illustrates all of the important |
| 769 |
issues you’ll need to consider when writing more useful functions.)</p> |
| 770 |
<p>Instead of continuing to copy-and-paste your code, you decide to wrap |
| 771 |
up the common behaviour in a function:</p> |
| 772 |
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a>threshold_x <span class="ot"><-</span> <span class="cf">function</span>(df, threshold) {</span> |
| 773 |
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a> <span class="fu">sieve</span>(df, <span class="sc">~</span> x <span class="sc">></span> threshold)</span> |
| 774 |
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a>}</span> |
| 775 |
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a><span class="fu">threshold_x</span>(df, <span class="dv">3</span>)</span> |
| 776 |
<span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 777 |
<span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a><span class="co">#> 4 4 2</span></span> |
| 778 |
<span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a><span class="co">#> 5 5 1</span></span></code></pre></div> |
| 779 |
<p>There are two ways that this function might fail:</p> |
| 780 |
<ol style="list-style-type: decimal"> |
| 781 |
<li><p>The data frame might not have a variable called <code>x</code>. |
| 782 |
This will fail unless there’s a variable called <code>x</code> hanging |
| 783 |
around in the global environment:</p> |
| 784 |
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" tabindex="-1"></a><span class="fu">rm</span>(x)</span> |
| 785 |
<span id="cb31-2"><a href="#cb31-2" tabindex="-1"></a>df2 <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">y =</span> <span class="dv">5</span><span class="sc">:</span><span class="dv">1</span>)</span> |
| 786 |
<span id="cb31-3"><a href="#cb31-3" tabindex="-1"></a></span> |
| 787 |
<span id="cb31-4"><a href="#cb31-4" tabindex="-1"></a><span class="co"># Throws an error</span></span> |
| 788 |
<span id="cb31-5"><a href="#cb31-5" tabindex="-1"></a><span class="fu">threshold_x</span>(df2, <span class="dv">3</span>)</span> |
| 789 |
<span id="cb31-6"><a href="#cb31-6" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 790 |
<span id="cb31-7"><a href="#cb31-7" tabindex="-1"></a><span class="co">#> ! object 'x' not found</span></span> |
| 791 |
<span id="cb31-8"><a href="#cb31-8" tabindex="-1"></a></span> |
| 792 |
<span id="cb31-9"><a href="#cb31-9" tabindex="-1"></a><span class="co"># Silently gives the incorrect result!</span></span> |
| 793 |
<span id="cb31-10"><a href="#cb31-10" tabindex="-1"></a>x <span class="ot"><-</span> <span class="dv">5</span></span> |
| 794 |
<span id="cb31-11"><a href="#cb31-11" tabindex="-1"></a><span class="fu">threshold_x</span>(df2, <span class="dv">3</span>)</span> |
| 795 |
<span id="cb31-12"><a href="#cb31-12" tabindex="-1"></a><span class="co">#> y</span></span> |
| 796 |
<span id="cb31-13"><a href="#cb31-13" tabindex="-1"></a><span class="co">#> 1 5</span></span> |
| 797 |
<span id="cb31-14"><a href="#cb31-14" tabindex="-1"></a><span class="co">#> 2 4</span></span> |
| 798 |
<span id="cb31-15"><a href="#cb31-15" tabindex="-1"></a><span class="co">#> 3 3</span></span> |
| 799 |
<span id="cb31-16"><a href="#cb31-16" tabindex="-1"></a><span class="co">#> 4 2</span></span> |
| 800 |
<span id="cb31-17"><a href="#cb31-17" tabindex="-1"></a><span class="co">#> 5 1</span></span></code></pre></div></li> |
| 801 |
<li><p>The data frame might have a variable called |
| 802 |
<code>threshold</code>:</p> |
| 803 |
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" tabindex="-1"></a>df3 <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">y =</span> <span class="dv">5</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">threshold =</span> <span class="dv">4</span>)</span> |
| 804 |
<span id="cb32-2"><a href="#cb32-2" tabindex="-1"></a><span class="fu">threshold_x</span>(df3, <span class="dv">3</span>)</span> |
| 805 |
<span id="cb32-3"><a href="#cb32-3" tabindex="-1"></a><span class="co">#> x y threshold</span></span> |
| 806 |
<span id="cb32-4"><a href="#cb32-4" tabindex="-1"></a><span class="co">#> 5 5 1 4</span></span></code></pre></div></li> |
| 807 |
</ol> |
| 808 |
<p>These failures are partiuclarly pernicious because instead of |
| 809 |
throwing an error they silently produce the wrong answer. Both failures |
| 810 |
arise because <code>f_eval()</code> introduces ambiguity by looking in |
| 811 |
two places for each name: the supplied data and formula environment.</p> |
| 812 |
<p>To make <code>threshold_x()</code> more reliable, we need to be more |
| 813 |
explicit by using the <code>.data</code> and <code>.env</code> |
| 814 |
pronouns:</p> |
| 815 |
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" tabindex="-1"></a>threshold_x <span class="ot"><-</span> <span class="cf">function</span>(df, threshold) {</span> |
| 816 |
<span id="cb33-2"><a href="#cb33-2" tabindex="-1"></a> <span class="fu">sieve</span>(df, <span class="sc">~</span> .data<span class="sc">$</span>x <span class="sc">></span> .env<span class="sc">$</span>threshold)</span> |
| 817 |
<span id="cb33-3"><a href="#cb33-3" tabindex="-1"></a>}</span> |
| 818 |
<span id="cb33-4"><a href="#cb33-4" tabindex="-1"></a></span> |
| 819 |
<span id="cb33-5"><a href="#cb33-5" tabindex="-1"></a><span class="fu">threshold_x</span>(df2, <span class="dv">3</span>)</span> |
| 820 |
<span id="cb33-6"><a href="#cb33-6" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 821 |
<span id="cb33-7"><a href="#cb33-7" tabindex="-1"></a><span class="co">#> ! Variable 'x' not found in data</span></span> |
| 822 |
<span id="cb33-8"><a href="#cb33-8" tabindex="-1"></a><span class="fu">threshold_x</span>(df3, <span class="dv">3</span>)</span> |
| 823 |
<span id="cb33-9"><a href="#cb33-9" tabindex="-1"></a><span class="co">#> x y threshold</span></span> |
| 824 |
<span id="cb33-10"><a href="#cb33-10" tabindex="-1"></a><span class="co">#> 4 4 2 4</span></span> |
| 825 |
<span id="cb33-11"><a href="#cb33-11" tabindex="-1"></a><span class="co">#> 5 5 1 4</span></span></code></pre></div> |
| 826 |
<p>Here <code>.env</code> is bound to the environment where |
| 827 |
<code>~</code> is evaluated, namely the inside of |
| 828 |
<code>threshold_x()</code>.</p> |
| 829 |
</div> |
| 830 |
<div id="adding-arguments" class="section level3"> |
| 831 |
<h3>Adding arguments</h3> |
| 832 |
<p>The <code>threshold_x()</code> function is not very useful because |
| 833 |
it’s bound to a specific variable. It would be more powerful if we could |
| 834 |
vary both the threshold and the variable it applies to. We can do that |
| 835 |
by taking an additional argument to specify which variable to use.</p> |
| 836 |
<p>One simple approach is to use a string and <code>[[</code>:</p> |
| 837 |
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" tabindex="-1"></a>threshold <span class="ot"><-</span> <span class="cf">function</span>(df, variable, threshold) {</span> |
| 838 |
<span id="cb34-2"><a href="#cb34-2" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="fu">is.character</span>(variable), <span class="fu">length</span>(variable) <span class="sc">==</span> <span class="dv">1</span>)</span> |
| 839 |
<span id="cb34-3"><a href="#cb34-3" tabindex="-1"></a> </span> |
| 840 |
<span id="cb34-4"><a href="#cb34-4" tabindex="-1"></a> <span class="fu">sieve</span>(df, <span class="sc">~</span> .data[[.env<span class="sc">$</span>variable]] <span class="sc">></span> .env<span class="sc">$</span>threshold)</span> |
| 841 |
<span id="cb34-5"><a href="#cb34-5" tabindex="-1"></a>}</span> |
| 842 |
<span id="cb34-6"><a href="#cb34-6" tabindex="-1"></a><span class="fu">threshold</span>(df, <span class="st">"x"</span>, <span class="dv">4</span>)</span> |
| 843 |
<span id="cb34-7"><a href="#cb34-7" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 844 |
<span id="cb34-8"><a href="#cb34-8" tabindex="-1"></a><span class="co">#> 5 5 1</span></span></code></pre></div> |
| 845 |
<p>This is a simple and robust solution, but only allows us to use an |
| 846 |
existing variable, not an arbitrary expression like |
| 847 |
<code>sqrt(x)</code>.</p> |
| 848 |
<p>A more general solution is to allow the user to supply a formula, and |
| 849 |
use unquoting:</p> |
| 850 |
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" tabindex="-1"></a>threshold <span class="ot"><-</span> <span class="cf">function</span>(df, <span class="at">variable =</span> <span class="sc">~</span>x, <span class="at">threshold =</span> <span class="dv">0</span>) {</span> |
| 851 |
<span id="cb35-2"><a href="#cb35-2" tabindex="-1"></a> <span class="fu">sieve</span>(df, <span class="sc">~</span> <span class="fu">uq</span>(variable) <span class="sc">></span> .env<span class="sc">$</span>threshold)</span> |
| 852 |
<span id="cb35-3"><a href="#cb35-3" tabindex="-1"></a>}</span> |
| 853 |
<span id="cb35-4"><a href="#cb35-4" tabindex="-1"></a></span> |
| 854 |
<span id="cb35-5"><a href="#cb35-5" tabindex="-1"></a><span class="fu">threshold</span>(df, <span class="sc">~</span> x, <span class="dv">4</span>)</span> |
| 855 |
<span id="cb35-6"><a href="#cb35-6" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 856 |
<span id="cb35-7"><a href="#cb35-7" tabindex="-1"></a><span class="co">#> 5 5 1</span></span> |
| 857 |
<span id="cb35-8"><a href="#cb35-8" tabindex="-1"></a><span class="fu">threshold</span>(df, <span class="sc">~</span> <span class="fu">abs</span>(x <span class="sc">-</span> y), <span class="dv">2</span>)</span> |
| 858 |
<span id="cb35-9"><a href="#cb35-9" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 859 |
<span id="cb35-10"><a href="#cb35-10" tabindex="-1"></a><span class="co">#> 1 1 5</span></span> |
| 860 |
<span id="cb35-11"><a href="#cb35-11" tabindex="-1"></a><span class="co">#> 5 5 1</span></span></code></pre></div> |
| 861 |
<p>In this case, it’s the responsibility of the user to ensure the |
| 862 |
<code>variable</code> is specified unambiguously. <code>f_eval()</code> |
| 863 |
is designed so that <code>.data</code> and <code>.env</code> work even |
| 864 |
when evaluated inside of <code>uq()</code>:</p> |
| 865 |
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="dv">3</span></span> |
| 866 |
<span id="cb36-2"><a href="#cb36-2" tabindex="-1"></a><span class="fu">threshold</span>(df, <span class="sc">~</span> .data<span class="sc">$</span>x <span class="sc">-</span> .env<span class="sc">$</span>x, <span class="dv">0</span>)</span> |
| 867 |
<span id="cb36-3"><a href="#cb36-3" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 868 |
<span id="cb36-4"><a href="#cb36-4" tabindex="-1"></a><span class="co">#> 4 4 2</span></span> |
| 869 |
<span id="cb36-5"><a href="#cb36-5" tabindex="-1"></a><span class="co">#> 5 5 1</span></span></code></pre></div> |
| 870 |
</div> |
| 871 |
<div id="dot-dot-dot" class="section level3"> |
| 872 |
<h3>Dot-dot-dot</h3> |
| 873 |
<p>There is one more tool that you might find useful for functions that |
| 874 |
take <code>...</code>. For example, the code below implements a function |
| 875 |
similar to <code>dplyr::mutate()</code> or |
| 876 |
<code>base::transform()</code>.</p> |
| 877 |
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" tabindex="-1"></a>mogrify <span class="ot"><-</span> <span class="cf">function</span>(<span class="st">`</span><span class="at">_df</span><span class="st">`</span>, ...) {</span> |
| 878 |
<span id="cb37-2"><a href="#cb37-2" tabindex="-1"></a> args <span class="ot"><-</span> <span class="fu">list</span>(...)</span> |
| 879 |
<span id="cb37-3"><a href="#cb37-3" tabindex="-1"></a> </span> |
| 880 |
<span id="cb37-4"><a href="#cb37-4" tabindex="-1"></a> <span class="cf">for</span> (nm <span class="cf">in</span> <span class="fu">names</span>(args)) {</span> |
| 881 |
<span id="cb37-5"><a href="#cb37-5" tabindex="-1"></a> <span class="st">`</span><span class="at">_df</span><span class="st">`</span>[[nm]] <span class="ot"><-</span> <span class="fu">f_eval</span>(args[[nm]], <span class="st">`</span><span class="at">_df</span><span class="st">`</span>)</span> |
| 882 |
<span id="cb37-6"><a href="#cb37-6" tabindex="-1"></a> }</span> |
| 883 |
<span id="cb37-7"><a href="#cb37-7" tabindex="-1"></a> </span> |
| 884 |
<span id="cb37-8"><a href="#cb37-8" tabindex="-1"></a> <span class="st">`</span><span class="at">_df</span><span class="st">`</span></span> |
| 885 |
<span id="cb37-9"><a href="#cb37-9" tabindex="-1"></a>}</span></code></pre></div> |
| 886 |
<p>(NB: the first argument is a non-syntactic name (i.e. it requires |
| 887 |
quoting with <code>`</code>) so it doesn’t accidentally match one of the |
| 888 |
names of the new variables.)</p> |
| 889 |
<p><code>transmogrifty()</code> makes it easy to add new variables to a |
| 890 |
data frame:</p> |
| 891 |
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">y =</span> <span class="fu">sample</span>(<span class="dv">5</span>))</span> |
| 892 |
<span id="cb38-2"><a href="#cb38-2" tabindex="-1"></a><span class="fu">mogrify</span>(df, <span class="at">z =</span> <span class="sc">~</span> x <span class="sc">+</span> y, <span class="at">z2 =</span> <span class="sc">~</span> z <span class="sc">*</span> <span class="dv">2</span>)</span> |
| 893 |
<span id="cb38-3"><a href="#cb38-3" tabindex="-1"></a><span class="co">#> x y z z2</span></span> |
| 894 |
<span id="cb38-4"><a href="#cb38-4" tabindex="-1"></a><span class="co">#> 1 1 3 4 8</span></span> |
| 895 |
<span id="cb38-5"><a href="#cb38-5" tabindex="-1"></a><span class="co">#> 2 2 5 7 14</span></span> |
| 896 |
<span id="cb38-6"><a href="#cb38-6" tabindex="-1"></a><span class="co">#> 3 3 1 4 8</span></span> |
| 897 |
<span id="cb38-7"><a href="#cb38-7" tabindex="-1"></a><span class="co">#> 4 4 4 8 16</span></span> |
| 898 |
<span id="cb38-8"><a href="#cb38-8" tabindex="-1"></a><span class="co">#> 5 5 2 7 14</span></span></code></pre></div> |
| 899 |
<p>One problem with this implementation is that it’s hard to specify the |
| 900 |
names of the generated variables. Imagine you want a function where the |
| 901 |
name and expression are in separate variables. This is awkward because |
| 902 |
the variable name is supplied as an argument name to |
| 903 |
<code>mogrify()</code>:</p> |
| 904 |
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" tabindex="-1"></a>add_variable <span class="ot"><-</span> <span class="cf">function</span>(df, name, expr) {</span> |
| 905 |
<span id="cb39-2"><a href="#cb39-2" tabindex="-1"></a> <span class="fu">do.call</span>(<span class="st">"mogrify"</span>, <span class="fu">c</span>(<span class="fu">list</span>(df), <span class="fu">setNames</span>(<span class="fu">list</span>(expr), name)))</span> |
| 906 |
<span id="cb39-3"><a href="#cb39-3" tabindex="-1"></a>}</span> |
| 907 |
<span id="cb39-4"><a href="#cb39-4" tabindex="-1"></a><span class="fu">add_variable</span>(df, <span class="st">"z"</span>, <span class="sc">~</span> x <span class="sc">+</span> y)</span> |
| 908 |
<span id="cb39-5"><a href="#cb39-5" tabindex="-1"></a><span class="co">#> x y z</span></span> |
| 909 |
<span id="cb39-6"><a href="#cb39-6" tabindex="-1"></a><span class="co">#> 1 1 3 4</span></span> |
| 910 |
<span id="cb39-7"><a href="#cb39-7" tabindex="-1"></a><span class="co">#> 2 2 5 7</span></span> |
| 911 |
<span id="cb39-8"><a href="#cb39-8" tabindex="-1"></a><span class="co">#> 3 3 1 4</span></span> |
| 912 |
<span id="cb39-9"><a href="#cb39-9" tabindex="-1"></a><span class="co">#> 4 4 4 8</span></span> |
| 913 |
<span id="cb39-10"><a href="#cb39-10" tabindex="-1"></a><span class="co">#> 5 5 2 7</span></span></code></pre></div> |
| 914 |
<p>Lazyeval provides the <code>f_list()</code> function to make writing |
| 915 |
this sort of function a little easier. It takes a list of formulas and |
| 916 |
evaluates the LHS of each formula (if present) to rename the |
| 917 |
elements:</p> |
| 918 |
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" tabindex="-1"></a><span class="fu">f_list</span>(<span class="st">"x"</span> <span class="sc">~</span> y, <span class="at">z =</span> <span class="sc">~</span>z)</span> |
| 919 |
<span id="cb40-2"><a href="#cb40-2" tabindex="-1"></a><span class="co">#> $x</span></span> |
| 920 |
<span id="cb40-3"><a href="#cb40-3" tabindex="-1"></a><span class="co">#> ~y</span></span> |
| 921 |
<span id="cb40-4"><a href="#cb40-4" tabindex="-1"></a><span class="co">#> </span></span> |
| 922 |
<span id="cb40-5"><a href="#cb40-5" tabindex="-1"></a><span class="co">#> $z</span></span> |
| 923 |
<span id="cb40-6"><a href="#cb40-6" tabindex="-1"></a><span class="co">#> ~z</span></span></code></pre></div> |
| 924 |
<p>If we tweak <code>mogrify()</code> to use <code>f_list()</code> |
| 925 |
instead of <code>list()</code>:</p> |
| 926 |
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" tabindex="-1"></a>mogrify <span class="ot"><-</span> <span class="cf">function</span>(<span class="st">`</span><span class="at">_df</span><span class="st">`</span>, ...) {</span> |
| 927 |
<span id="cb41-2"><a href="#cb41-2" tabindex="-1"></a> args <span class="ot"><-</span> <span class="fu">f_list</span>(...)</span> |
| 928 |
<span id="cb41-3"><a href="#cb41-3" tabindex="-1"></a> </span> |
| 929 |
<span id="cb41-4"><a href="#cb41-4" tabindex="-1"></a> <span class="cf">for</span> (nm <span class="cf">in</span> <span class="fu">names</span>(args)) {</span> |
| 930 |
<span id="cb41-5"><a href="#cb41-5" tabindex="-1"></a> <span class="st">`</span><span class="at">_df</span><span class="st">`</span>[[nm]] <span class="ot"><-</span> <span class="fu">f_eval</span>(args[[nm]], <span class="st">`</span><span class="at">_df</span><span class="st">`</span>)</span> |
| 931 |
<span id="cb41-6"><a href="#cb41-6" tabindex="-1"></a> }</span> |
| 932 |
<span id="cb41-7"><a href="#cb41-7" tabindex="-1"></a> </span> |
| 933 |
<span id="cb41-8"><a href="#cb41-8" tabindex="-1"></a> <span class="st">`</span><span class="at">_df</span><span class="st">`</span></span> |
| 934 |
<span id="cb41-9"><a href="#cb41-9" tabindex="-1"></a>}</span></code></pre></div> |
| 935 |
<p><code>add_new()</code> becomes much simpler:</p> |
| 936 |
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" tabindex="-1"></a>add_variable <span class="ot"><-</span> <span class="cf">function</span>(df, name, expr) {</span> |
| 937 |
<span id="cb42-2"><a href="#cb42-2" tabindex="-1"></a> <span class="fu">mogrify</span>(df, name <span class="sc">~</span> <span class="fu">uq</span>(expr))</span> |
| 938 |
<span id="cb42-3"><a href="#cb42-3" tabindex="-1"></a>}</span> |
| 939 |
<span id="cb42-4"><a href="#cb42-4" tabindex="-1"></a><span class="fu">add_variable</span>(df, <span class="st">"z"</span>, <span class="sc">~</span> x <span class="sc">+</span> y)</span> |
| 940 |
<span id="cb42-5"><a href="#cb42-5" tabindex="-1"></a><span class="co">#> x y z</span></span> |
| 941 |
<span id="cb42-6"><a href="#cb42-6" tabindex="-1"></a><span class="co">#> 1 1 3 4</span></span> |
| 942 |
<span id="cb42-7"><a href="#cb42-7" tabindex="-1"></a><span class="co">#> 2 2 5 7</span></span> |
| 943 |
<span id="cb42-8"><a href="#cb42-8" tabindex="-1"></a><span class="co">#> 3 3 1 4</span></span> |
| 944 |
<span id="cb42-9"><a href="#cb42-9" tabindex="-1"></a><span class="co">#> 4 4 4 8</span></span> |
| 945 |
<span id="cb42-10"><a href="#cb42-10" tabindex="-1"></a><span class="co">#> 5 5 2 7</span></span></code></pre></div> |
| 946 |
</div> |
| 947 |
<div id="exercises-2" class="section level3"> |
| 948 |
<h3>Exercises</h3> |
| 949 |
<ol style="list-style-type: decimal"> |
| 950 |
<li><p>Write a function that selects all rows of <code>df</code> where |
| 951 |
<code>variable</code> is greater than its mean. Make the function more |
| 952 |
general by allowing the user to specify a function to use instead of |
| 953 |
<code>mean()</code> (e.g. <code>median()</code>).</p></li> |
| 954 |
<li><p>Create a version of <code>mogrify()</code> where the first |
| 955 |
argument is <code>x</code>? What happens if you try to create a new |
| 956 |
variable called <code>x</code>?</p></li> |
| 957 |
</ol> |
| 958 |
</div> |
| 959 |
</div> |
| 960 |
<div id="non-standard-evaluation" class="section level2"> |
| 961 |
<h2>Non-standard evaluation</h2> |
| 962 |
<p>In some situations you might want to eliminate the formula |
| 963 |
altogether, and allow the user to type expressions directly. I was once |
| 964 |
much enamoured with this approach (witness ggplot2, dplyr, …). However, |
| 965 |
I now think that it should be used sparingly because explict quoting |
| 966 |
with <code>~</code> leads to simpler code, and makes it more clear to |
| 967 |
the user that something special is going on.</p> |
| 968 |
<p>That said, lazyeval does allow you to eliminate the <code>~</code> if |
| 969 |
you really want to. In this case, I recommend having both a NSE and SE |
| 970 |
version of the function. The SE version, which takes formuals, should |
| 971 |
have suffix <code>_</code>:</p> |
| 972 |
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" tabindex="-1"></a>sieve_ <span class="ot"><-</span> <span class="cf">function</span>(df, condition) {</span> |
| 973 |
<span id="cb43-2"><a href="#cb43-2" tabindex="-1"></a> rows <span class="ot"><-</span> <span class="fu">f_eval</span>(condition, df)</span> |
| 974 |
<span id="cb43-3"><a href="#cb43-3" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">is.logical</span>(rows)) {</span> |
| 975 |
<span id="cb43-4"><a href="#cb43-4" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"`condition` must be logical."</span>, <span class="at">call. =</span> <span class="cn">FALSE</span>)</span> |
| 976 |
<span id="cb43-5"><a href="#cb43-5" tabindex="-1"></a> }</span> |
| 977 |
<span id="cb43-6"><a href="#cb43-6" tabindex="-1"></a> </span> |
| 978 |
<span id="cb43-7"><a href="#cb43-7" tabindex="-1"></a> rows[<span class="fu">is.na</span>(rows)] <span class="ot"><-</span> <span class="cn">FALSE</span></span> |
| 979 |
<span id="cb43-8"><a href="#cb43-8" tabindex="-1"></a> df[rows, , drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span> |
| 980 |
<span id="cb43-9"><a href="#cb43-9" tabindex="-1"></a>}</span></code></pre></div> |
| 981 |
<p>Then create the NSE version which doesn’t need the explicit formula. |
| 982 |
The key is the use of <code>f_capture()</code> which takes an |
| 983 |
unevaluated argument (a promise) and captures it as a formula:</p> |
| 984 |
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" tabindex="-1"></a>sieve <span class="ot"><-</span> <span class="cf">function</span>(df, expr) {</span> |
| 985 |
<span id="cb44-2"><a href="#cb44-2" tabindex="-1"></a> <span class="fu">sieve_</span>(df, <span class="fu">f_capture</span>(expr))</span> |
| 986 |
<span id="cb44-3"><a href="#cb44-3" tabindex="-1"></a>}</span> |
| 987 |
<span id="cb44-4"><a href="#cb44-4" tabindex="-1"></a><span class="fu">sieve</span>(df, x <span class="sc">==</span> <span class="dv">1</span>)</span> |
| 988 |
<span id="cb44-5"><a href="#cb44-5" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 989 |
<span id="cb44-6"><a href="#cb44-6" tabindex="-1"></a><span class="co">#> 1 1 3</span></span></code></pre></div> |
| 990 |
<p>If you’re familiar with <code>substitute()</code> you might expect |
| 991 |
the same drawbacks to apply. However, <code>f_capture()</code> is smart |
| 992 |
enough to follow a chain of promises back to the original value, so, for |
| 993 |
example, this code works fine:</p> |
| 994 |
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" tabindex="-1"></a>scramble <span class="ot"><-</span> <span class="cf">function</span>(df) {</span> |
| 995 |
<span id="cb45-2"><a href="#cb45-2" tabindex="-1"></a> df[<span class="fu">sample</span>(<span class="fu">nrow</span>(df)), , drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span> |
| 996 |
<span id="cb45-3"><a href="#cb45-3" tabindex="-1"></a>}</span> |
| 997 |
<span id="cb45-4"><a href="#cb45-4" tabindex="-1"></a>subscramble <span class="ot"><-</span> <span class="cf">function</span>(df, expr) {</span> |
| 998 |
<span id="cb45-5"><a href="#cb45-5" tabindex="-1"></a> <span class="fu">scramble</span>(<span class="fu">sieve</span>(df, expr))</span> |
| 999 |
<span id="cb45-6"><a href="#cb45-6" tabindex="-1"></a>}</span> |
| 1000 |
<span id="cb45-7"><a href="#cb45-7" tabindex="-1"></a><span class="fu">subscramble</span>(df, x <span class="sc"><</span> <span class="dv">4</span>)</span> |
| 1001 |
<span id="cb45-8"><a href="#cb45-8" tabindex="-1"></a><span class="co">#> x y</span></span> |
| 1002 |
<span id="cb45-9"><a href="#cb45-9" tabindex="-1"></a><span class="co">#> 2 2 5</span></span> |
| 1003 |
<span id="cb45-10"><a href="#cb45-10" tabindex="-1"></a><span class="co">#> 1 1 3</span></span> |
| 1004 |
<span id="cb45-11"><a href="#cb45-11" tabindex="-1"></a><span class="co">#> 3 3 1</span></span></code></pre></div> |
| 1005 |
<div id="dot-dot-dot-1" class="section level3"> |
| 1006 |
<h3>Dot-dot-dot</h3> |
| 1007 |
<p>If you want a <code>...</code> function that doesn’t require |
| 1008 |
formulas, I recommend that the SE version take a list of arguments, and |
| 1009 |
the NSE version uses <code>dots_capture()</code> to capture multiple |
| 1010 |
arguments as a list of formulas.</p> |
| 1011 |
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" tabindex="-1"></a>mogrify_ <span class="ot"><-</span> <span class="cf">function</span>(<span class="st">`</span><span class="at">_df</span><span class="st">`</span>, args) {</span> |
| 1012 |
<span id="cb46-2"><a href="#cb46-2" tabindex="-1"></a> args <span class="ot"><-</span> <span class="fu">as_f_list</span>(args)</span> |
| 1013 |
<span id="cb46-3"><a href="#cb46-3" tabindex="-1"></a> </span> |
| 1014 |
<span id="cb46-4"><a href="#cb46-4" tabindex="-1"></a> <span class="cf">for</span> (nm <span class="cf">in</span> <span class="fu">names</span>(args)) {</span> |
| 1015 |
<span id="cb46-5"><a href="#cb46-5" tabindex="-1"></a> <span class="st">`</span><span class="at">_df</span><span class="st">`</span>[[nm]] <span class="ot"><-</span> <span class="fu">f_eval</span>(args[[nm]], <span class="st">`</span><span class="at">_df</span><span class="st">`</span>)</span> |
| 1016 |
<span id="cb46-6"><a href="#cb46-6" tabindex="-1"></a> }</span> |
| 1017 |
<span id="cb46-7"><a href="#cb46-7" tabindex="-1"></a> </span> |
| 1018 |
<span id="cb46-8"><a href="#cb46-8" tabindex="-1"></a> <span class="st">`</span><span class="at">_df</span><span class="st">`</span></span> |
| 1019 |
<span id="cb46-9"><a href="#cb46-9" tabindex="-1"></a>}</span> |
| 1020 |
<span id="cb46-10"><a href="#cb46-10" tabindex="-1"></a></span> |
| 1021 |
<span id="cb46-11"><a href="#cb46-11" tabindex="-1"></a>mogrify <span class="ot"><-</span> <span class="cf">function</span>(<span class="st">`</span><span class="at">_df</span><span class="st">`</span>, ...) {</span> |
| 1022 |
<span id="cb46-12"><a href="#cb46-12" tabindex="-1"></a> <span class="fu">mogrify_</span>(<span class="st">`</span><span class="at">_df</span><span class="st">`</span>, <span class="fu">dots_capture</span>(...))</span> |
| 1023 |
<span id="cb46-13"><a href="#cb46-13" tabindex="-1"></a>}</span></code></pre></div> |
| 1024 |
</div> |
| 1025 |
<div id="exercises-3" class="section level3"> |
| 1026 |
<h3>Exercises</h3> |
| 1027 |
<ol style="list-style-type: decimal"> |
| 1028 |
<li>Recreate <code>subscramble()</code> using |
| 1029 |
<code>base::subset()</code> instead of <code>sieve()</code>. Why does it |
| 1030 |
fail?</li> |
| 1031 |
</ol> |
| 1032 |
</div> |
| 1033 |
</div> |
| 1034 |
<div id="metaprogramming" class="section level2"> |
| 1035 |
<h2>Metaprogramming</h2> |
| 1036 |
<p>The final use of non-standard evaluation is to do metaprogramming. |
| 1037 |
This is a catch-all term that encompasses any function that does |
| 1038 |
computation on an unevaluated expression. You can learn about |
| 1039 |
metaprogrgramming in <a href="http://adv-r.had.co.nz/Expressions.html" class="uri">http://adv-r.had.co.nz/Expressions.html</a>, particularly <a href="http://adv-r.had.co.nz/Expressions.html#ast-funs" class="uri">http://adv-r.had.co.nz/Expressions.html#ast-funs</a>. Over |
| 1040 |
time, the goal is to move all useful metaprogramming helper functions |
| 1041 |
into this package, and discuss metaprogramming more here.</p> |
| 1042 |
</div> |
| 1043 |
<div class="footnotes footnotes-end-of-document"> |
| 1044 |
<hr /> |
| 1045 |
<ol> |
| 1046 |
<li id="fn1"><p>Currently neither ggplot2 nor dplyr actually use these |
| 1047 |
tools since I’ve only just figured it out. But I’ll be working hard to |
| 1048 |
make sure all my packages are consistent in the near future.<a href="#fnref1" class="footnote-back">↩︎</a></p></li> |
| 1049 |
</ol> |
| 1050 |
</div> |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
<script> |
| 1059 |
(function () { |
| 1060 |
var script = document.createElement("script"); |
| 1061 |
script.type = "text/javascript"; |
| 1062 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 1063 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 1064 |
})(); |
| 1065 |
</script> |
| 1066 |
|
| 1067 |
</body> |
| 1068 |
</html> |