File: lazyeval.html

package info (click to toggle)
r-cran-lazyeval 0.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 596 kB
  • sloc: ansic: 310; sh: 9; makefile: 2
file content (836 lines) | stat: -rw-r--r-- 96,021 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />

<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="author" content="Hadley Wickham" />

<meta name="date" content="2019-03-15" />

<title>Non-standard evaluation</title>



<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(data-line-number);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */

</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
  var sheets = document.styleSheets;
  for (var i = 0; i < sheets.length; i++) {
    if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
    try { var rules = sheets[i].cssRules; } catch (e) { continue; }
    for (var j = 0; j < rules.length; j++) {
      var rule = rules[j];
      // check if there is a div.sourceCode rule
      if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
      var style = rule.style.cssText;
      // check if color or background-color is set
      if (rule.style.color === '' || rule.style.backgroundColor === '') continue;
      // replace div.sourceCode by a pre.sourceCode rule
      sheets[i].deleteRule(j);
      sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
    }
  }
})();
</script>



<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#header {
text-align: center;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap; 
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }

code > span.kw { color: #555; font-weight: bold; } 
code > span.dt { color: #902000; } 
code > span.dv { color: #40a070; } 
code > span.bn { color: #d14; } 
code > span.fl { color: #d14; } 
code > span.ch { color: #d14; } 
code > span.st { color: #d14; } 
code > span.co { color: #888888; font-style: italic; } 
code > span.ot { color: #007020; } 
code > span.al { color: #ff0000; font-weight: bold; } 
code > span.fu { color: #900; font-weight: bold; }  code > span.er { color: #a61717; background-color: #e3d2d2; } 
</style>

</head>

<body>




<h1 class="title toc-ignore">Non-standard evaluation</h1>
<h4 class="author"><em>Hadley Wickham</em></h4>
<h4 class="date"><em>2019-03-15</em></h4>



<p>This document describes lazyeval, a package that provides principled tools to perform non-standard evaluation (NSE) in R. You should read this vignette if you want to program with packages like dplyr and ggplot2<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>, or you want a principled way of working with delayed expressions in your own package. As the name suggests, non-standard evaluation breaks away from the standard evaluation (SE) rules in order to do something special. There are three common uses of NSE:</p>
<ol style="list-style-type: decimal">
<li><p><strong>Labelling</strong> enhances plots and tables by using the expressions supplied to a function, rather than their values. For example, note the axis labels in this plot:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="kw">par</span>(<span class="dt">mar =</span> <span class="kw">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>))</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">grid &lt;-<span class="st"> </span><span class="kw">seq</span>(<span class="dv">0</span>, <span class="dv">2</span> <span class="op">*</span><span class="st"> </span>pi, <span class="dt">length =</span> <span class="dv">100</span>)</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="kw">plot</span>(grid, <span class="kw">sin</span>(grid), <span class="dt">type =</span> <span class="st">&quot;l&quot;</span>)</a></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAADwCAYAAAAJkrPKAAAEGWlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPrtzZyMkzlNsNI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GEsiXL5+0atVKunTpoozAs2fPlrJly8rRo0clQYIEPl8MHj8w+rZr105gWD5w4IB07NjR5/N5IAlEJ0AvoOhE+J0ETCAwbdo0SZcunSxcuFDmzJkjzZo1k2LFisnevXt9vhrOX7RokXTo0EGyZs0qiRMnlj59+sjx48d9roMHkoA3gQS6G9q/fmjee/iZBEjAEAKYo4chGFM4MNp6SsOGDZVtYPHixZ5NPr9jbUDKlCklefLkPp/DA0kgOgFOAUUnwu8kYDAB+PfDDjBo0CDBZ5Tly5fLkiVLpG7dugFdLVOmTLz5B0SOJ3kT4AjAmwY/k4BJBD788EO1ChireJMlSyZnz56V119/XcaOHWvSFVktCcRPgAogfkY8ggQMIQBX0K1bt8qFCxckIiJCsBiMhQSsJEAFYCV9XpsESIAELCRAG4CF8HlpEiABErCSABWAlfR5bRIgARKwkAAVgIXweWkSIAESsJIAFYCV9HltEiABErCQABWAhfB5aRIgARKwkgAVgJX0eW0SIAESsJAAFYCF8HlpEiABErCSABWAlfR5bRIgARKwkMD/A/OrLDu49NDyAAAAAElFTkSuQmCC" /><!-- --></p></li>
<li><p><strong>Non-standard scoping</strong> looks for objects in places other than the current environment. For example, base R has <code>with()</code>, <code>subset()</code>, and <code>transform()</code> that look for objects in a data frame (or list) before the current environment:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1">df &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x =</span> <span class="kw">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="dt">y =</span> <span class="kw">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>))</a>
<a class="sourceLine" id="cb2-2" data-line-number="2"></a>
<a class="sourceLine" id="cb2-3" data-line-number="3"><span class="kw">with</span>(df, <span class="kw">mean</span>(x))</a>
<a class="sourceLine" id="cb2-4" data-line-number="4"><span class="co">#&gt; [1] 3</span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5"><span class="kw">subset</span>(df, x <span class="op">==</span><span class="st"> </span>y)</a>
<a class="sourceLine" id="cb2-6" data-line-number="6"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb2-7" data-line-number="7"><span class="co">#&gt; 5 3 3</span></a>
<a class="sourceLine" id="cb2-8" data-line-number="8"><span class="kw">transform</span>(df, <span class="dt">z =</span> x <span class="op">+</span><span class="st"> </span>y)</a>
<a class="sourceLine" id="cb2-9" data-line-number="9"><span class="co">#&gt;   x y z</span></a>
<a class="sourceLine" id="cb2-10" data-line-number="10"><span class="co">#&gt; 1 1 2 3</span></a>
<a class="sourceLine" id="cb2-11" data-line-number="11"><span class="co">#&gt; 2 5 1 6</span></a>
<a class="sourceLine" id="cb2-12" data-line-number="12"><span class="co">#&gt; 3 4 5 9</span></a>
<a class="sourceLine" id="cb2-13" data-line-number="13"><span class="co">#&gt; 4 2 4 6</span></a>
<a class="sourceLine" id="cb2-14" data-line-number="14"><span class="co">#&gt; 5 3 3 6</span></a></code></pre></div></li>
<li><p><strong>Metaprogramming</strong> is a catch-all term that covers all other uses of NSE (such as in <code>bquote()</code> and <code>library()</code>). Metaprogramming is so called because it involves computing on the unevaluated code in some way.</p></li>
</ol>
<p>This document is broadly organised according to the three types of non-standard evaluation described above. The main difference is that after <a href="#labelling">labelling</a>, we’ll take a detour to learn more about <a href="#formulas">formulas</a>. You’re probably familiar with formulas from linear models (e.g. <code>lm(mpg ~ displ, data = mtcars)</code>) but formulas are more than just a tool for modelling: they are a general way of capturing an unevaluated expression.</p>
<p>The approaches recommended here are quite different to my previous generation of recommendations. I am fairly confident these new approaches are correct, and will not have to change substantially again. The current tools make it easy to solve a number of practical problems that were previously challenging and are rooted in <a href="http://repository.readscheme.org/ftp/papers/pepm99/bawden.pdf">long-standing theory</a>.</p>
<div id="labelling" class="section level2">
<h2>Labelling</h2>
<p>In base R, the classic way to turn an argument into a label is to use <code>deparse(substitute(x))</code>:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1">my_label &lt;-<span class="st"> </span><span class="cf">function</span>(x) <span class="kw">deparse</span>(<span class="kw">substitute</span>(x))</a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="kw">my_label</span>(x <span class="op">+</span><span class="st"> </span>y)</a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="co">#&gt; [1] &quot;x + y&quot;</span></a></code></pre></div>
<p>There are two potential problems with this approach:</p>
<ol style="list-style-type: decimal">
<li><p>For long some expressions, <code>deparse()</code> generates a character vector with length &gt; 1:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="kw">my_label</span>({</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">  a <span class="op">+</span><span class="st"> </span>b</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">  c <span class="op">+</span><span class="st"> </span>d</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">})</a>
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="co">#&gt; [1] &quot;{&quot;         &quot;    a + b&quot; &quot;    c + d&quot; &quot;}&quot;</span></a></code></pre></div></li>
<li><p><code>substitute()</code> only looks one level up, so you lose the original label if the function isn’t called directly:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" data-line-number="1">my_label2 &lt;-<span class="st"> </span><span class="cf">function</span>(x) <span class="kw">my_label</span>(x)</a>
<a class="sourceLine" id="cb5-2" data-line-number="2"><span class="kw">my_label2</span>(a <span class="op">+</span><span class="st"> </span>b)</a>
<a class="sourceLine" id="cb5-3" data-line-number="3"><span class="co">#&gt; [1] &quot;x&quot;</span></a></code></pre></div></li>
</ol>
<p>Both of these problems are resolved by <code>lazyeval::expr_text()</code>:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb6-1" data-line-number="1">my_label &lt;-<span class="st"> </span><span class="cf">function</span>(x) <span class="kw">expr_text</span>(x)</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">my_label2 &lt;-<span class="st"> </span><span class="cf">function</span>(x) <span class="kw">my_label</span>(x)</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">   </a>
<a class="sourceLine" id="cb6-4" data-line-number="4"><span class="kw">my_label</span>({</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">  a <span class="op">+</span><span class="st"> </span>b</a>
<a class="sourceLine" id="cb6-6" data-line-number="6">  c <span class="op">+</span><span class="st"> </span>d</a>
<a class="sourceLine" id="cb6-7" data-line-number="7">})</a>
<a class="sourceLine" id="cb6-8" data-line-number="8"><span class="co">#&gt; [1] &quot;{\n    a + b\n    c + d\n}&quot;</span></a>
<a class="sourceLine" id="cb6-9" data-line-number="9"><span class="kw">my_label2</span>(a <span class="op">+</span><span class="st"> </span>b)</a>
<a class="sourceLine" id="cb6-10" data-line-number="10"><span class="co">#&gt; [1] &quot;a + b&quot;</span></a></code></pre></div>
<p>There are two variations on the theme of <code>expr_text()</code>:</p>
<ul>
<li><p><code>expr_find()</code> find the underlying expression. It works similarly to <code>substitute()</code> but will follow a chain of promises back up to the original expression. This is often useful for <a href="#metaprogramming">metaprogramming</a>.</p></li>
<li><p><code>expr_label()</code> is a customised version of <code>expr_text()</code> that produces labels designed to be used in messages to the user:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="kw">expr_label</span>(x)</a>
<a class="sourceLine" id="cb7-2" data-line-number="2"><span class="co">#&gt; [1] &quot;`x`&quot;</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3"><span class="kw">expr_label</span>(a <span class="op">+</span><span class="st"> </span>b <span class="op">+</span><span class="st"> </span>c)</a>
<a class="sourceLine" id="cb7-4" data-line-number="4"><span class="co">#&gt; [1] &quot;`a + b + c`&quot;</span></a>
<a class="sourceLine" id="cb7-5" data-line-number="5"><span class="kw">expr_label</span>(<span class="kw">foo</span>({</a>
<a class="sourceLine" id="cb7-6" data-line-number="6">  x <span class="op">+</span><span class="st"> </span>y</a>
<a class="sourceLine" id="cb7-7" data-line-number="7">}))</a>
<a class="sourceLine" id="cb7-8" data-line-number="8"><span class="co">#&gt; [1] &quot;`foo(...)`&quot;</span></a></code></pre></div></li>
</ul>
<div id="exercises" class="section level3">
<h3>Exercises</h3>
<ol style="list-style-type: decimal">
<li><p><code>plot()</code> uses <code>deparse(substitute(x))</code> to generate labels for the x and y axes. Can you generate input that causes it to display bad labels? Write your own wrapper around <code>plot()</code> that uses <code>expr_label()</code> to compute <code>xlim</code> and <code>ylim</code>.</p></li>
<li><p>Create a simple implementation of <code>mean()</code> that stops with an informative error message if the argument is not numeric:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1">x &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;c&quot;</span>)</a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="kw">my_mean</span>(x)</a>
<a class="sourceLine" id="cb8-3" data-line-number="3"><span class="co">#&gt; Error: `x` is a not a numeric vector.</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"><span class="kw">my_mean</span>(x <span class="op">==</span><span class="st"> &quot;a&quot;</span>)</a>
<a class="sourceLine" id="cb8-5" data-line-number="5"><span class="co">#&gt; Error: `x == &quot;a&quot;` is not a numeric vector.</span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6"><span class="kw">my_mean</span>(<span class="st">&quot;a&quot;</span>)</a>
<a class="sourceLine" id="cb8-7" data-line-number="7"><span class="co">#&gt; Error: &quot;a&quot; is not a numeric vector.</span></a></code></pre></div></li>
<li><p>Read the source code for <code>expr_text()</code>. How does it work? What additional arguments to <code>deparse()</code> does it use?</p></li>
</ol>
</div>
</div>
<div id="formulas" class="section level2">
<h2>Formulas</h2>
<p>Non-standard scoping is probably the most useful NSE tool, but before we can talk about a solid approach, we need to take a detour to talk about formulas. Formulas are a familiar tool from linear models, but their utility is not limited to models. In fact, formulas are a powerful, general purpose tool, because a formula captures two things:</p>
<ol style="list-style-type: decimal">
<li>An unevaluated expression.</li>
<li>The context (environment) in which the expression was created.</li>
</ol>
<p><code>~</code> is a single character that allows you to say: “I want to capture the meaning of this code, without evaluating it right away”. For that reason, the formula can be thought of as a “quoting” operator.</p>
<div id="definition-of-a-formula" class="section level3">
<h3>Definition of a formula</h3>
<p>Technically, a formula is a “language” object (i.e. an unevaluated expression) with a class of “formula” and an attribute that stores the environment:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb9-1" data-line-number="1">f &lt;-<span class="st"> </span><span class="er">~</span><span class="st"> </span>x <span class="op">+</span><span class="st"> </span>y <span class="op">+</span><span class="st"> </span>z</a>
<a class="sourceLine" id="cb9-2" data-line-number="2"><span class="kw">typeof</span>(f)</a>
<a class="sourceLine" id="cb9-3" data-line-number="3"><span class="co">#&gt; [1] &quot;language&quot;</span></a>
<a class="sourceLine" id="cb9-4" data-line-number="4"><span class="kw">attributes</span>(f)</a>
<a class="sourceLine" id="cb9-5" data-line-number="5"><span class="co">#&gt; $class</span></a>
<a class="sourceLine" id="cb9-6" data-line-number="6"><span class="co">#&gt; [1] &quot;formula&quot;</span></a>
<a class="sourceLine" id="cb9-7" data-line-number="7"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb9-8" data-line-number="8"><span class="co">#&gt; $.Environment</span></a>
<a class="sourceLine" id="cb9-9" data-line-number="9"><span class="co">#&gt; &lt;environment: R_GlobalEnv&gt;</span></a></code></pre></div>
<p>The structure of the underlying object is slightly different depending on whether you have a one-sided or two-sided formula:</p>
<ul>
<li><p>One-sided formulas have length two:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="kw">length</span>(f)</a>
<a class="sourceLine" id="cb10-2" data-line-number="2"><span class="co">#&gt; [1] 2</span></a>
<a class="sourceLine" id="cb10-3" data-line-number="3"><span class="co"># The 1st element is always ~</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">f[[<span class="dv">1</span>]]</a>
<a class="sourceLine" id="cb10-5" data-line-number="5"><span class="co">#&gt; `~`</span></a>
<a class="sourceLine" id="cb10-6" data-line-number="6"><span class="co"># The 2nd element is the RHS</span></a>
<a class="sourceLine" id="cb10-7" data-line-number="7">f[[<span class="dv">2</span>]]</a>
<a class="sourceLine" id="cb10-8" data-line-number="8"><span class="co">#&gt; x + y + z</span></a></code></pre></div></li>
<li><p>Two-sided formulas have length three:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb11-1" data-line-number="1">g &lt;-<span class="st"> </span>y <span class="op">~</span><span class="st"> </span>x <span class="op">+</span><span class="st"> </span>z</a>
<a class="sourceLine" id="cb11-2" data-line-number="2"><span class="kw">length</span>(g)</a>
<a class="sourceLine" id="cb11-3" data-line-number="3"><span class="co">#&gt; [1] 3</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4"><span class="co"># The 1st element is still ~</span></a>
<a class="sourceLine" id="cb11-5" data-line-number="5">g[[<span class="dv">1</span>]]</a>
<a class="sourceLine" id="cb11-6" data-line-number="6"><span class="co">#&gt; `~`</span></a>
<a class="sourceLine" id="cb11-7" data-line-number="7"><span class="co"># But now the 2nd element is the LHS</span></a>
<a class="sourceLine" id="cb11-8" data-line-number="8">g[[<span class="dv">2</span>]]</a>
<a class="sourceLine" id="cb11-9" data-line-number="9"><span class="co">#&gt; y</span></a>
<a class="sourceLine" id="cb11-10" data-line-number="10"><span class="co"># And the 3rd element is the RHS</span></a>
<a class="sourceLine" id="cb11-11" data-line-number="11">g[[<span class="dv">3</span>]]</a>
<a class="sourceLine" id="cb11-12" data-line-number="12"><span class="co">#&gt; x + z</span></a></code></pre></div></li>
</ul>
<p>To abstract away these differences, lazyeval provides <code>f_rhs()</code> and <code>f_lhs()</code> to access either side of the formula, and <code>f_env()</code> to access its environment:</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="kw">f_rhs</span>(f)</a>
<a class="sourceLine" id="cb12-2" data-line-number="2"><span class="co">#&gt; x + y + z</span></a>
<a class="sourceLine" id="cb12-3" data-line-number="3"><span class="kw">f_lhs</span>(f)</a>
<a class="sourceLine" id="cb12-4" data-line-number="4"><span class="co">#&gt; NULL</span></a>
<a class="sourceLine" id="cb12-5" data-line-number="5"><span class="kw">f_env</span>(f)</a>
<a class="sourceLine" id="cb12-6" data-line-number="6"><span class="co">#&gt; &lt;environment: R_GlobalEnv&gt;</span></a>
<a class="sourceLine" id="cb12-7" data-line-number="7"></a>
<a class="sourceLine" id="cb12-8" data-line-number="8"><span class="kw">f_rhs</span>(g)</a>
<a class="sourceLine" id="cb12-9" data-line-number="9"><span class="co">#&gt; x + z</span></a>
<a class="sourceLine" id="cb12-10" data-line-number="10"><span class="kw">f_lhs</span>(g)</a>
<a class="sourceLine" id="cb12-11" data-line-number="11"><span class="co">#&gt; y</span></a>
<a class="sourceLine" id="cb12-12" data-line-number="12"><span class="kw">f_env</span>(g)</a>
<a class="sourceLine" id="cb12-13" data-line-number="13"><span class="co">#&gt; &lt;environment: R_GlobalEnv&gt;</span></a></code></pre></div>
</div>
<div id="evaluating-a-formula" class="section level3">
<h3>Evaluating a formula</h3>
<p>A formula captures delays the evaluation of an expression so you can later evaluate it with <code>f_eval()</code>:</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb13-1" data-line-number="1">f &lt;-<span class="st"> </span><span class="er">~</span><span class="st"> </span><span class="dv">1</span> <span class="op">+</span><span class="st"> </span><span class="dv">2</span> <span class="op">+</span><span class="st"> </span><span class="dv">3</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2">f</a>
<a class="sourceLine" id="cb13-3" data-line-number="3"><span class="co">#&gt; ~1 + 2 + 3</span></a>
<a class="sourceLine" id="cb13-4" data-line-number="4"><span class="kw">f_eval</span>(f)</a>
<a class="sourceLine" id="cb13-5" data-line-number="5"><span class="co">#&gt; [1] 6</span></a></code></pre></div>
<p>This allows you to use a formula as a robust way of delaying evaluation, cleanly separating the creation of the formula from its evaluation. Because formulas capture the code and context, you get the correct result even when a formula is created and evaluated in different places. In the following example, note that the value of <code>x</code> inside <code>add_1000()</code> is used:</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb14-1" data-line-number="1">x &lt;-<span class="st"> </span><span class="dv">1</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">add_<span class="dv">1000</span> &lt;-<span class="st"> </span><span class="cf">function</span>(x) {</a>
<a class="sourceLine" id="cb14-3" data-line-number="3">  <span class="op">~</span><span class="st"> </span><span class="dv">1000</span> <span class="op">+</span><span class="st"> </span>x</a>
<a class="sourceLine" id="cb14-4" data-line-number="4">}</a>
<a class="sourceLine" id="cb14-5" data-line-number="5"></a>
<a class="sourceLine" id="cb14-6" data-line-number="6"><span class="kw">add_1000</span>(<span class="dv">3</span>)</a>
<a class="sourceLine" id="cb14-7" data-line-number="7"><span class="co">#&gt; ~1000 + x</span></a>
<a class="sourceLine" id="cb14-8" data-line-number="8"><span class="co">#&gt; &lt;environment: 0x7ff857bfaa98&gt;</span></a>
<a class="sourceLine" id="cb14-9" data-line-number="9"><span class="kw">f_eval</span>(<span class="kw">add_1000</span>(<span class="dv">3</span>))</a>
<a class="sourceLine" id="cb14-10" data-line-number="10"><span class="co">#&gt; [1] 1003</span></a></code></pre></div>
<p>It can be hard to see what’s going on when looking at a formula because important values are stored in the environment, which is largely opaque. You can use <code>f_unwrap()</code> to replace names with their corresponding values:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="kw">f_unwrap</span>(<span class="kw">add_1000</span>(<span class="dv">3</span>))</a>
<a class="sourceLine" id="cb15-2" data-line-number="2"><span class="co">#&gt; ~1000 + 3</span></a></code></pre></div>
</div>
<div id="non-standard-scoping" class="section level3">
<h3>Non-standard scoping</h3>
<p><code>f_eval()</code> has an optional second argument: a named list (or data frame) that overrides values found in the formula’s environment.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb16-1" data-line-number="1">y &lt;-<span class="st"> </span><span class="dv">100</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span>y)</a>
<a class="sourceLine" id="cb16-3" data-line-number="3"><span class="co">#&gt; [1] 100</span></a>
<a class="sourceLine" id="cb16-4" data-line-number="4"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span>y, <span class="dt">data =</span> <span class="kw">list</span>(<span class="dt">y =</span> <span class="dv">10</span>))</a>
<a class="sourceLine" id="cb16-5" data-line-number="5"><span class="co">#&gt; [1] 10</span></a>
<a class="sourceLine" id="cb16-6" data-line-number="6"></a>
<a class="sourceLine" id="cb16-7" data-line-number="7"><span class="co"># Can mix variables in environment and data argument</span></a>
<a class="sourceLine" id="cb16-8" data-line-number="8"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span>x <span class="op">+</span><span class="st"> </span>y, <span class="dt">data =</span> <span class="kw">list</span>(<span class="dt">x =</span> <span class="dv">10</span>))</a>
<a class="sourceLine" id="cb16-9" data-line-number="9"><span class="co">#&gt; [1] 110</span></a>
<a class="sourceLine" id="cb16-10" data-line-number="10"><span class="co"># Can even supply functions</span></a>
<a class="sourceLine" id="cb16-11" data-line-number="11"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span><span class="kw">f</span>(y), <span class="dt">data =</span> <span class="kw">list</span>(<span class="dt">f =</span> <span class="cf">function</span>(x) x <span class="op">*</span><span class="st"> </span><span class="dv">3</span>))</a>
<a class="sourceLine" id="cb16-12" data-line-number="12"><span class="co">#&gt; [1] 300</span></a></code></pre></div>
<p>This makes it very easy to implement non-standard scoping:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span><span class="kw">mean</span>(cyl), <span class="dt">data =</span> mtcars)</a>
<a class="sourceLine" id="cb17-2" data-line-number="2"><span class="co">#&gt; [1] 6.1875</span></a></code></pre></div>
<p>One challenge with non-standard scoping is that we’ve introduced some ambiguity. For example, in the code below does <code>x</code> come from <code>mydata</code> or the environment?</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span>x, <span class="dt">data =</span> mydata)</a></code></pre></div>
<p>You can’t tell without knowing whether or not <code>mydata</code> has a variable called <code>x</code>. To overcome this problem, <code>f_eval()</code> provides two pronouns:</p>
<ul>
<li><code>.data</code> is bound to the data frame.</li>
<li><code>.env</code> is bound to the formula environment.</li>
</ul>
<p>They both start with <code>.</code> to minimise the chances of clashing with existing variables.</p>
<p>With these pronouns we can rewrite the previous formula to remove the ambiguity:</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb19-1" data-line-number="1">mydata &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x =</span> <span class="dv">100</span>, <span class="dt">y =</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb19-2" data-line-number="2">x &lt;-<span class="st"> </span><span class="dv">10</span></a>
<a class="sourceLine" id="cb19-3" data-line-number="3"></a>
<a class="sourceLine" id="cb19-4" data-line-number="4"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span>.env<span class="op">$</span>x, <span class="dt">data =</span> mydata)</a>
<a class="sourceLine" id="cb19-5" data-line-number="5"><span class="co">#&gt; [1] 10</span></a>
<a class="sourceLine" id="cb19-6" data-line-number="6"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span>.data<span class="op">$</span>x, <span class="dt">data =</span> mydata)</a>
<a class="sourceLine" id="cb19-7" data-line-number="7"><span class="co">#&gt; [1] 100</span></a></code></pre></div>
<p>If the variable or object doesn’t exist, you’ll get an informative error:</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span>.env<span class="op">$</span>z, <span class="dt">data =</span> mydata)</a>
<a class="sourceLine" id="cb20-2" data-line-number="2"><span class="co">#&gt; Error: Object 'z' not found in environment</span></a>
<a class="sourceLine" id="cb20-3" data-line-number="3"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span>.data<span class="op">$</span>z, <span class="dt">data =</span> mydata)</a>
<a class="sourceLine" id="cb20-4" data-line-number="4"><span class="co">#&gt; Error: Variable 'z' not found in data</span></a></code></pre></div>
</div>
<div id="unquoting" class="section level3">
<h3>Unquoting</h3>
<p><code>f_eval()</code> has one more useful trick up its sleeve: unquoting. Unquoting allows you to write functions where the user supplies part of the formula. For example, the following function allows you to compute the mean of any column (or any function of a column):</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb21-1" data-line-number="1">df_mean &lt;-<span class="st"> </span><span class="cf">function</span>(df, variable) {</a>
<a class="sourceLine" id="cb21-2" data-line-number="2">  <span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span><span class="kw">mean</span>(<span class="kw">uq</span>(variable)), <span class="dt">data =</span> df)</a>
<a class="sourceLine" id="cb21-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb21-4" data-line-number="4"></a>
<a class="sourceLine" id="cb21-5" data-line-number="5"><span class="kw">df_mean</span>(mtcars, <span class="op">~</span><span class="st"> </span>cyl)</a>
<a class="sourceLine" id="cb21-6" data-line-number="6"><span class="co">#&gt; [1] 6.1875</span></a>
<a class="sourceLine" id="cb21-7" data-line-number="7"><span class="kw">df_mean</span>(mtcars, <span class="op">~</span><span class="st"> </span>disp <span class="op">*</span><span class="st"> </span><span class="fl">0.01638</span>)</a>
<a class="sourceLine" id="cb21-8" data-line-number="8"><span class="co">#&gt; [1] 3.779224</span></a>
<a class="sourceLine" id="cb21-9" data-line-number="9"><span class="kw">df_mean</span>(mtcars, <span class="op">~</span><span class="st"> </span><span class="kw">sqrt</span>(mpg))</a>
<a class="sourceLine" id="cb21-10" data-line-number="10"><span class="co">#&gt; [1] 4.43477</span></a></code></pre></div>
<p>To see how this works, we can use <code>f_interp()</code> which <code>f_eval()</code> calls internally (you shouldn’t call it in your own code, but it’s useful for debugging). The key is <code>uq()</code>: <code>uq()</code> evaluates its first (and only) argument and inserts the value into the formula:</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb22-1" data-line-number="1">variable &lt;-<span class="st"> </span><span class="er">~</span>cyl</a>
<a class="sourceLine" id="cb22-2" data-line-number="2"><span class="kw">f_interp</span>(<span class="op">~</span><span class="st"> </span><span class="kw">mean</span>(<span class="kw">uq</span>(variable)))</a>
<a class="sourceLine" id="cb22-3" data-line-number="3"><span class="co">#&gt; ~mean(cyl)</span></a>
<a class="sourceLine" id="cb22-4" data-line-number="4"></a>
<a class="sourceLine" id="cb22-5" data-line-number="5">variable &lt;-<span class="st"> </span><span class="er">~</span><span class="st"> </span>disp <span class="op">*</span><span class="st"> </span><span class="fl">0.01638</span></a>
<a class="sourceLine" id="cb22-6" data-line-number="6"><span class="kw">f_interp</span>(<span class="op">~</span><span class="st"> </span><span class="kw">mean</span>(<span class="kw">uq</span>(variable)))</a>
<a class="sourceLine" id="cb22-7" data-line-number="7"><span class="co">#&gt; ~mean(disp * 0.01638)</span></a></code></pre></div>
<p>Unquoting allows you to create code “templates”, where you write most of the expression, while still allowing the user to control important components. You can even use <code>uq()</code> to change the function being called:</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb23-1" data-line-number="1">f &lt;-<span class="st"> </span><span class="er">~</span><span class="st"> </span>mean</a>
<a class="sourceLine" id="cb23-2" data-line-number="2"><span class="kw">f_interp</span>(<span class="op">~</span><span class="st"> </span><span class="kw">uq</span>(f)(<span class="kw">uq</span>(variable)))</a>
<a class="sourceLine" id="cb23-3" data-line-number="3"><span class="co">#&gt; ~mean(disp * 0.01638)</span></a></code></pre></div>
<p>Note that <code>uq()</code> only takes the RHS of a formula, which makes it difficult to insert literal formulas into a call:</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb24-1" data-line-number="1">formula &lt;-<span class="st"> </span>y <span class="op">~</span><span class="st"> </span>x</a>
<a class="sourceLine" id="cb24-2" data-line-number="2"><span class="kw">f_interp</span>(<span class="op">~</span><span class="st"> </span><span class="kw">lm</span>(<span class="kw">uq</span>(formula), <span class="dt">data =</span> df))</a>
<a class="sourceLine" id="cb24-3" data-line-number="3"><span class="co">#&gt; ~lm(x, data = df)</span></a></code></pre></div>
<p>You can instead use <code>uqf()</code> which uses the whole formula, not just the RHS:</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb25-1" data-line-number="1"><span class="kw">f_interp</span>(<span class="op">~</span><span class="st"> </span><span class="kw">lm</span>(<span class="kw">uqf</span>(formula), <span class="dt">data =</span> df))</a>
<a class="sourceLine" id="cb25-2" data-line-number="2"><span class="co">#&gt; ~lm(y ~ x, data = df)</span></a></code></pre></div>
<p>Unquoting is powerful, but it only allows you to modify a single argument: it doesn’t allow you to add an arbitrary number of arguments. To do that, you’ll need “unquote-splice”, or <code>uqs()</code>. The first (and only) argument to <code>uqs()</code> should be a list of arguments to be spliced into the call:</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb26-1" data-line-number="1">variable &lt;-<span class="st"> </span><span class="er">~</span><span class="st"> </span>x</a>
<a class="sourceLine" id="cb26-2" data-line-number="2">extra_args &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dt">na.rm =</span> <span class="ot">TRUE</span>, <span class="dt">trim =</span> <span class="fl">0.9</span>)</a>
<a class="sourceLine" id="cb26-3" data-line-number="3"><span class="kw">f_interp</span>(<span class="op">~</span><span class="st"> </span><span class="kw">mean</span>(<span class="kw">uq</span>(variable), <span class="kw">uqs</span>(extra_args)))</a>
<a class="sourceLine" id="cb26-4" data-line-number="4"><span class="co">#&gt; ~mean(x, na.rm = TRUE, trim = 0.9)</span></a></code></pre></div>
</div>
<div id="exercises-1" class="section level3">
<h3>Exercises</h3>
<ol style="list-style-type: decimal">
<li><p>Create a wrapper around <code>lm()</code> that allows the user to supply the response and predictors as two separate formulas.</p></li>
<li><p>Compare and contrast <code>f_eval()</code> with <code>with()</code>.</p></li>
<li><p>Why does this code work even though <code>f</code> is defined in two places? (And one of them is not a function).</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb27-1" data-line-number="1">f &lt;-<span class="st"> </span><span class="cf">function</span>(x) x <span class="op">+</span><span class="st"> </span><span class="dv">1</span></a>
<a class="sourceLine" id="cb27-2" data-line-number="2"><span class="kw">f_eval</span>(<span class="op">~</span><span class="st"> </span><span class="kw">f</span>(<span class="dv">10</span>), <span class="kw">list</span>(<span class="dt">f =</span> <span class="st">&quot;a&quot;</span>))</a>
<a class="sourceLine" id="cb27-3" data-line-number="3"><span class="co">#&gt; [1] 11</span></a></code></pre></div></li>
</ol>
</div>
</div>
<div id="non-standard-scoping-1" class="section level2">
<h2>Non-standard scoping</h2>
<p>Non-standard scoping (NSS) is an important part of R because it makes it easy to write functions tailored for interactive data exploration. These functions require less typing, at the cost of some ambiguity and “magic”. This is a good trade-off for interactive data exploration because you want to get ideas out of your head and into the computer as quickly as possible. If a function does make a bad guess, you’ll spot it quickly because you’re working interactively.</p>
<p>There are three challenges to implementing non-standard scoping:</p>
<ol style="list-style-type: decimal">
<li><p>You must correctly delay the evaluation of a function argument, capturing both the computation (the expression), and the context (the environment). I recommend making this explicit by requiring the user to “quote” any NSS arguments with <code>~</code>, and then evaluating explicit with <code>f_eval()</code>.</p></li>
<li><p>When writing functions that use NSS-functions, you need some way to avoid the automatic lookup and be explicit about where objects should be found. <code>f_eval()</code> solves this problem with the <code>.data.</code> and <code>.env</code> pronouns.</p></li>
<li><p>You need some way to allow the user to supply parts of a formula. <code>f_eval()</code> solves this with unquoting.</p></li>
</ol>
<p>To illustrate these challenges, I will implement a <code>sieve()</code> function that works similarly to <code>base::subset()</code> or <code>dplyr::filter()</code>. The goal of <code>sieve()</code> is to make it easy to select observations that match criteria defined by a logical expression. <code>sieve()</code> has three advantages over <code>[</code>:</p>
<ol style="list-style-type: decimal">
<li><p>It is much more compact when the condition uses many variables, because you don’t need to repeat the name of the data frame many times.</p></li>
<li><p>It drops rows where the condition evaluates to <code>NA</code>, rather than filling them with <code>NA</code>s.</p></li>
<li><p>It always returns a data frame.</p></li>
</ol>
<p>The implementation of <code>sieve()</code> is straightforward. First we use <code>f_eval()</code> to perform NSS. Then we then check that we have a logical vector, replace <code>NA</code>s with <code>FALSE</code>, and subset with <code>[</code>.</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb28-1" data-line-number="1">sieve &lt;-<span class="st"> </span><span class="cf">function</span>(df, condition) {</a>
<a class="sourceLine" id="cb28-2" data-line-number="2">  rows &lt;-<span class="st"> </span><span class="kw">f_eval</span>(condition, df)</a>
<a class="sourceLine" id="cb28-3" data-line-number="3">  <span class="cf">if</span> (<span class="op">!</span><span class="kw">is.logical</span>(rows)) {</a>
<a class="sourceLine" id="cb28-4" data-line-number="4">    <span class="kw">stop</span>(<span class="st">&quot;`condition` must be logical.&quot;</span>, <span class="dt">call. =</span> <span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb28-5" data-line-number="5">  }</a>
<a class="sourceLine" id="cb28-6" data-line-number="6">  </a>
<a class="sourceLine" id="cb28-7" data-line-number="7">  rows[<span class="kw">is.na</span>(rows)] &lt;-<span class="st"> </span><span class="ot">FALSE</span></a>
<a class="sourceLine" id="cb28-8" data-line-number="8">  df[rows, , drop =<span class="st"> </span><span class="ot">FALSE</span>]</a>
<a class="sourceLine" id="cb28-9" data-line-number="9">}</a>
<a class="sourceLine" id="cb28-10" data-line-number="10"></a>
<a class="sourceLine" id="cb28-11" data-line-number="11">df &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">5</span>, <span class="dt">y =</span> <span class="dv">5</span><span class="op">:</span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb28-12" data-line-number="12"><span class="kw">sieve</span>(df, <span class="op">~</span><span class="st"> </span>x <span class="op">&lt;=</span><span class="st"> </span><span class="dv">2</span>)</a>
<a class="sourceLine" id="cb28-13" data-line-number="13"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb28-14" data-line-number="14"><span class="co">#&gt; 1 1 5</span></a>
<a class="sourceLine" id="cb28-15" data-line-number="15"><span class="co">#&gt; 2 2 4</span></a>
<a class="sourceLine" id="cb28-16" data-line-number="16"><span class="kw">sieve</span>(df, <span class="op">~</span><span class="st"> </span>x <span class="op">==</span><span class="st"> </span>y)</a>
<a class="sourceLine" id="cb28-17" data-line-number="17"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb28-18" data-line-number="18"><span class="co">#&gt; 3 3 3</span></a></code></pre></div>
<div id="programming-with-sieve" class="section level3">
<h3>Programming with <code>sieve()</code></h3>
<p>Imagine that you’ve written some code that looks like this:</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb29-1" data-line-number="1"><span class="kw">sieve</span>(march, <span class="op">~</span><span class="st"> </span>x <span class="op">&gt;</span><span class="st"> </span><span class="dv">100</span>)</a>
<a class="sourceLine" id="cb29-2" data-line-number="2"><span class="kw">sieve</span>(april, <span class="op">~</span><span class="st"> </span>x <span class="op">&gt;</span><span class="st"> </span><span class="dv">50</span>)</a>
<a class="sourceLine" id="cb29-3" data-line-number="3"><span class="kw">sieve</span>(june, <span class="op">~</span><span class="st"> </span>x <span class="op">&gt;</span><span class="st"> </span><span class="dv">45</span>)</a>
<a class="sourceLine" id="cb29-4" data-line-number="4"><span class="kw">sieve</span>(july, <span class="op">~</span><span class="st"> </span>x <span class="op">&gt;</span><span class="st"> </span><span class="dv">17</span>)</a></code></pre></div>
<p>(This is a contrived example, but it illustrates all of the important issues you’ll need to consider when writing more useful functions.)</p>
<p>Instead of continuing to copy-and-paste your code, you decide to wrap up the common behaviour in a function:</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb30-1" data-line-number="1">threshold_x &lt;-<span class="st"> </span><span class="cf">function</span>(df, threshold) {</a>
<a class="sourceLine" id="cb30-2" data-line-number="2">  <span class="kw">sieve</span>(df, <span class="op">~</span><span class="st"> </span>x <span class="op">&gt;</span><span class="st"> </span>threshold)</a>
<a class="sourceLine" id="cb30-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb30-4" data-line-number="4"><span class="kw">threshold_x</span>(df, <span class="dv">3</span>)</a>
<a class="sourceLine" id="cb30-5" data-line-number="5"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb30-6" data-line-number="6"><span class="co">#&gt; 4 4 2</span></a>
<a class="sourceLine" id="cb30-7" data-line-number="7"><span class="co">#&gt; 5 5 1</span></a></code></pre></div>
<p>There are two ways that this function might fail:</p>
<ol style="list-style-type: decimal">
<li><p>The data frame might not have a variable called <code>x</code>. This will fail unless there’s a variable called <code>x</code> hanging around in the global environment:</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb31-1" data-line-number="1"><span class="kw">rm</span>(x)</a>
<a class="sourceLine" id="cb31-2" data-line-number="2">df2 &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">y =</span> <span class="dv">5</span><span class="op">:</span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb31-3" data-line-number="3"></a>
<a class="sourceLine" id="cb31-4" data-line-number="4"><span class="co"># Throws an error</span></a>
<a class="sourceLine" id="cb31-5" data-line-number="5"><span class="kw">threshold_x</span>(df2, <span class="dv">3</span>)</a>
<a class="sourceLine" id="cb31-6" data-line-number="6"><span class="co">#&gt; Error in eval(expr, data, expr_env): object 'x' not found</span></a>
<a class="sourceLine" id="cb31-7" data-line-number="7"></a>
<a class="sourceLine" id="cb31-8" data-line-number="8"><span class="co"># Silently gives the incorrect result!</span></a>
<a class="sourceLine" id="cb31-9" data-line-number="9">x &lt;-<span class="st"> </span><span class="dv">5</span></a>
<a class="sourceLine" id="cb31-10" data-line-number="10"><span class="kw">threshold_x</span>(df2, <span class="dv">3</span>)</a>
<a class="sourceLine" id="cb31-11" data-line-number="11"><span class="co">#&gt;   y</span></a>
<a class="sourceLine" id="cb31-12" data-line-number="12"><span class="co">#&gt; 1 5</span></a>
<a class="sourceLine" id="cb31-13" data-line-number="13"><span class="co">#&gt; 2 4</span></a>
<a class="sourceLine" id="cb31-14" data-line-number="14"><span class="co">#&gt; 3 3</span></a>
<a class="sourceLine" id="cb31-15" data-line-number="15"><span class="co">#&gt; 4 2</span></a>
<a class="sourceLine" id="cb31-16" data-line-number="16"><span class="co">#&gt; 5 1</span></a></code></pre></div></li>
<li><p>The data frame might have a variable called <code>threshold</code>:</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb32-1" data-line-number="1">df3 &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">5</span>, <span class="dt">y =</span> <span class="dv">5</span><span class="op">:</span><span class="dv">1</span>, <span class="dt">threshold =</span> <span class="dv">4</span>)</a>
<a class="sourceLine" id="cb32-2" data-line-number="2"><span class="kw">threshold_x</span>(df3, <span class="dv">3</span>)</a>
<a class="sourceLine" id="cb32-3" data-line-number="3"><span class="co">#&gt;   x y threshold</span></a>
<a class="sourceLine" id="cb32-4" data-line-number="4"><span class="co">#&gt; 5 5 1         4</span></a></code></pre></div></li>
</ol>
<p>These failures are partiuclarly pernicious because instead of throwing an error they silently produce the wrong answer. Both failures arise because <code>f_eval()</code> introduces ambiguity by looking in two places for each name: the supplied data and formula environment.</p>
<p>To make <code>threshold_x()</code> more reliable, we need to be more explicit by using the <code>.data</code> and <code>.env</code> pronouns:</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb33-1" data-line-number="1">threshold_x &lt;-<span class="st"> </span><span class="cf">function</span>(df, threshold) {</a>
<a class="sourceLine" id="cb33-2" data-line-number="2">  <span class="kw">sieve</span>(df, <span class="op">~</span><span class="st"> </span>.data<span class="op">$</span>x <span class="op">&gt;</span><span class="st"> </span>.env<span class="op">$</span>threshold)</a>
<a class="sourceLine" id="cb33-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb33-4" data-line-number="4"></a>
<a class="sourceLine" id="cb33-5" data-line-number="5"><span class="kw">threshold_x</span>(df2, <span class="dv">3</span>)</a>
<a class="sourceLine" id="cb33-6" data-line-number="6"><span class="co">#&gt; Error: Variable 'x' not found in data</span></a>
<a class="sourceLine" id="cb33-7" data-line-number="7"><span class="kw">threshold_x</span>(df3, <span class="dv">3</span>)</a>
<a class="sourceLine" id="cb33-8" data-line-number="8"><span class="co">#&gt;   x y threshold</span></a>
<a class="sourceLine" id="cb33-9" data-line-number="9"><span class="co">#&gt; 4 4 2         4</span></a>
<a class="sourceLine" id="cb33-10" data-line-number="10"><span class="co">#&gt; 5 5 1         4</span></a></code></pre></div>
<p>Here <code>.env</code> is bound to the environment where <code>~</code> is evaluated, namely the inside of <code>threshold_x()</code>.</p>
</div>
<div id="adding-arguments" class="section level3">
<h3>Adding arguments</h3>
<p>The <code>threshold_x()</code> function is not very useful because it’s bound to a specific variable. It would be more powerful if we could vary both the threshold and the variable it applies to. We can do that by taking an additional argument to specify which variable to use.</p>
<p>One simple approach is to use a string and <code>[[</code>:</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb34-1" data-line-number="1">threshold &lt;-<span class="st"> </span><span class="cf">function</span>(df, variable, threshold) {</a>
<a class="sourceLine" id="cb34-2" data-line-number="2">  <span class="kw">stopifnot</span>(<span class="kw">is.character</span>(variable), <span class="kw">length</span>(variable) <span class="op">==</span><span class="st"> </span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb34-3" data-line-number="3">  </a>
<a class="sourceLine" id="cb34-4" data-line-number="4">  <span class="kw">sieve</span>(df, <span class="op">~</span><span class="st"> </span>.data[[.env<span class="op">$</span>variable]] <span class="op">&gt;</span><span class="st"> </span>.env<span class="op">$</span>threshold)</a>
<a class="sourceLine" id="cb34-5" data-line-number="5">}</a>
<a class="sourceLine" id="cb34-6" data-line-number="6"><span class="kw">threshold</span>(df, <span class="st">&quot;x&quot;</span>, <span class="dv">4</span>)</a>
<a class="sourceLine" id="cb34-7" data-line-number="7"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb34-8" data-line-number="8"><span class="co">#&gt; 5 5 1</span></a></code></pre></div>
<p>This is a simple and robust solution, but only allows us to use an existing variable, not an arbitrary expression like <code>sqrt(x)</code>.</p>
<p>A more general solution is to allow the user to supply a formula, and use unquoting:</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb35-1" data-line-number="1">threshold &lt;-<span class="st"> </span><span class="cf">function</span>(df, <span class="dt">variable =</span> <span class="op">~</span>x, <span class="dt">threshold =</span> <span class="dv">0</span>) {</a>
<a class="sourceLine" id="cb35-2" data-line-number="2">  <span class="kw">sieve</span>(df, <span class="op">~</span><span class="st"> </span><span class="kw">uq</span>(variable) <span class="op">&gt;</span><span class="st"> </span>.env<span class="op">$</span>threshold)</a>
<a class="sourceLine" id="cb35-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb35-4" data-line-number="4"></a>
<a class="sourceLine" id="cb35-5" data-line-number="5"><span class="kw">threshold</span>(df, <span class="op">~</span><span class="st"> </span>x, <span class="dv">4</span>)</a>
<a class="sourceLine" id="cb35-6" data-line-number="6"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb35-7" data-line-number="7"><span class="co">#&gt; 5 5 1</span></a>
<a class="sourceLine" id="cb35-8" data-line-number="8"><span class="kw">threshold</span>(df, <span class="op">~</span><span class="st"> </span><span class="kw">abs</span>(x <span class="op">-</span><span class="st"> </span>y), <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb35-9" data-line-number="9"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb35-10" data-line-number="10"><span class="co">#&gt; 1 1 5</span></a>
<a class="sourceLine" id="cb35-11" data-line-number="11"><span class="co">#&gt; 5 5 1</span></a></code></pre></div>
<p>In this case, it’s the responsibility of the user to ensure the <code>variable</code> is specified unambiguously. <code>f_eval()</code> is designed so that <code>.data</code> and <code>.env</code> work even when evaluated inside of <code>uq()</code>:</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb36-1" data-line-number="1">x &lt;-<span class="st"> </span><span class="dv">3</span></a>
<a class="sourceLine" id="cb36-2" data-line-number="2"><span class="kw">threshold</span>(df, <span class="op">~</span><span class="st"> </span>.data<span class="op">$</span>x <span class="op">-</span><span class="st"> </span>.env<span class="op">$</span>x, <span class="dv">0</span>)</a>
<a class="sourceLine" id="cb36-3" data-line-number="3"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb36-4" data-line-number="4"><span class="co">#&gt; 4 4 2</span></a>
<a class="sourceLine" id="cb36-5" data-line-number="5"><span class="co">#&gt; 5 5 1</span></a></code></pre></div>
</div>
<div id="dot-dot-dot" class="section level3">
<h3>Dot-dot-dot</h3>
<p>There is one more tool that you might find useful for functions that take <code>...</code>. For example, the code below implements a function similar to <code>dplyr::mutate()</code> or <code>base::transform()</code>.</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb37-1" data-line-number="1">mogrify &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="st">`</span><span class="dt">_df</span><span class="st">`</span>, ...) {</a>
<a class="sourceLine" id="cb37-2" data-line-number="2">  args &lt;-<span class="st"> </span><span class="kw">list</span>(...)</a>
<a class="sourceLine" id="cb37-3" data-line-number="3">  </a>
<a class="sourceLine" id="cb37-4" data-line-number="4">  <span class="cf">for</span> (nm <span class="cf">in</span> <span class="kw">names</span>(args)) {</a>
<a class="sourceLine" id="cb37-5" data-line-number="5">    <span class="st">`</span><span class="dt">_df</span><span class="st">`</span>[[nm]] &lt;-<span class="st"> </span><span class="kw">f_eval</span>(args[[nm]], <span class="st">`</span><span class="dt">_df</span><span class="st">`</span>)</a>
<a class="sourceLine" id="cb37-6" data-line-number="6">  }</a>
<a class="sourceLine" id="cb37-7" data-line-number="7">  </a>
<a class="sourceLine" id="cb37-8" data-line-number="8">  <span class="st">`</span><span class="dt">_df</span><span class="st">`</span></a>
<a class="sourceLine" id="cb37-9" data-line-number="9">}</a></code></pre></div>
<p>(NB: the first argument is a non-syntactic name (i.e. it requires quoting with <code>`</code>) so it doesn’t accidentally match one of the names of the new variables.)</p>
<p><code>transmogrifty()</code> makes it easy to add new variables to a data frame:</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb38-1" data-line-number="1">df &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">5</span>, <span class="dt">y =</span> <span class="kw">sample</span>(<span class="dv">5</span>))</a>
<a class="sourceLine" id="cb38-2" data-line-number="2"><span class="kw">mogrify</span>(df, <span class="dt">z =</span> <span class="op">~</span><span class="st"> </span>x <span class="op">+</span><span class="st"> </span>y, <span class="dt">z2 =</span> <span class="op">~</span><span class="st"> </span>z <span class="op">*</span><span class="st"> </span><span class="dv">2</span>)</a>
<a class="sourceLine" id="cb38-3" data-line-number="3"><span class="co">#&gt;   x y z z2</span></a>
<a class="sourceLine" id="cb38-4" data-line-number="4"><span class="co">#&gt; 1 1 2 3  6</span></a>
<a class="sourceLine" id="cb38-5" data-line-number="5"><span class="co">#&gt; 2 2 5 7 14</span></a>
<a class="sourceLine" id="cb38-6" data-line-number="6"><span class="co">#&gt; 3 3 3 6 12</span></a>
<a class="sourceLine" id="cb38-7" data-line-number="7"><span class="co">#&gt; 4 4 1 5 10</span></a>
<a class="sourceLine" id="cb38-8" data-line-number="8"><span class="co">#&gt; 5 5 4 9 18</span></a></code></pre></div>
<p>One problem with this implementation is that it’s hard to specify the names of the generated variables. Imagine you want a function where the name and expression are in separate variables. This is awkward because the variable name is supplied as an argument name to <code>mogrify()</code>:</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb39-1" data-line-number="1">add_variable &lt;-<span class="st"> </span><span class="cf">function</span>(df, name, expr) {</a>
<a class="sourceLine" id="cb39-2" data-line-number="2">  <span class="kw">do.call</span>(<span class="st">&quot;mogrify&quot;</span>, <span class="kw">c</span>(<span class="kw">list</span>(df), <span class="kw">setNames</span>(<span class="kw">list</span>(expr), name)))</a>
<a class="sourceLine" id="cb39-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb39-4" data-line-number="4"><span class="kw">add_variable</span>(df, <span class="st">&quot;z&quot;</span>, <span class="op">~</span><span class="st"> </span>x <span class="op">+</span><span class="st"> </span>y)</a>
<a class="sourceLine" id="cb39-5" data-line-number="5"><span class="co">#&gt;   x y z</span></a>
<a class="sourceLine" id="cb39-6" data-line-number="6"><span class="co">#&gt; 1 1 2 3</span></a>
<a class="sourceLine" id="cb39-7" data-line-number="7"><span class="co">#&gt; 2 2 5 7</span></a>
<a class="sourceLine" id="cb39-8" data-line-number="8"><span class="co">#&gt; 3 3 3 6</span></a>
<a class="sourceLine" id="cb39-9" data-line-number="9"><span class="co">#&gt; 4 4 1 5</span></a>
<a class="sourceLine" id="cb39-10" data-line-number="10"><span class="co">#&gt; 5 5 4 9</span></a></code></pre></div>
<p>Lazyeval provides the <code>f_list()</code> function to make writing this sort of function a little easier. It takes a list of formulas and evaluates the LHS of each formula (if present) to rename the elements:</p>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb40-1" data-line-number="1"><span class="kw">f_list</span>(<span class="st">&quot;x&quot;</span> <span class="op">~</span><span class="st"> </span>y, <span class="dt">z =</span> <span class="op">~</span>z)</a>
<a class="sourceLine" id="cb40-2" data-line-number="2"><span class="co">#&gt; $x</span></a>
<a class="sourceLine" id="cb40-3" data-line-number="3"><span class="co">#&gt; ~y</span></a>
<a class="sourceLine" id="cb40-4" data-line-number="4"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb40-5" data-line-number="5"><span class="co">#&gt; $z</span></a>
<a class="sourceLine" id="cb40-6" data-line-number="6"><span class="co">#&gt; ~z</span></a></code></pre></div>
<p>If we tweak <code>mogrify()</code> to use <code>f_list()</code> instead of <code>list()</code>:</p>
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb41-1" data-line-number="1">mogrify &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="st">`</span><span class="dt">_df</span><span class="st">`</span>, ...) {</a>
<a class="sourceLine" id="cb41-2" data-line-number="2">  args &lt;-<span class="st"> </span><span class="kw">f_list</span>(...)</a>
<a class="sourceLine" id="cb41-3" data-line-number="3">  </a>
<a class="sourceLine" id="cb41-4" data-line-number="4">  <span class="cf">for</span> (nm <span class="cf">in</span> <span class="kw">names</span>(args)) {</a>
<a class="sourceLine" id="cb41-5" data-line-number="5">    <span class="st">`</span><span class="dt">_df</span><span class="st">`</span>[[nm]] &lt;-<span class="st"> </span><span class="kw">f_eval</span>(args[[nm]], <span class="st">`</span><span class="dt">_df</span><span class="st">`</span>)</a>
<a class="sourceLine" id="cb41-6" data-line-number="6">  }</a>
<a class="sourceLine" id="cb41-7" data-line-number="7">  </a>
<a class="sourceLine" id="cb41-8" data-line-number="8">  <span class="st">`</span><span class="dt">_df</span><span class="st">`</span></a>
<a class="sourceLine" id="cb41-9" data-line-number="9">}</a></code></pre></div>
<p><code>add_new()</code> becomes much simpler:</p>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb42-1" data-line-number="1">add_variable &lt;-<span class="st"> </span><span class="cf">function</span>(df, name, expr) {</a>
<a class="sourceLine" id="cb42-2" data-line-number="2">  <span class="kw">mogrify</span>(df, name <span class="op">~</span><span class="st"> </span><span class="kw">uq</span>(expr))</a>
<a class="sourceLine" id="cb42-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb42-4" data-line-number="4"><span class="kw">add_variable</span>(df, <span class="st">&quot;z&quot;</span>, <span class="op">~</span><span class="st"> </span>x <span class="op">+</span><span class="st"> </span>y)</a>
<a class="sourceLine" id="cb42-5" data-line-number="5"><span class="co">#&gt;   x y z</span></a>
<a class="sourceLine" id="cb42-6" data-line-number="6"><span class="co">#&gt; 1 1 2 3</span></a>
<a class="sourceLine" id="cb42-7" data-line-number="7"><span class="co">#&gt; 2 2 5 7</span></a>
<a class="sourceLine" id="cb42-8" data-line-number="8"><span class="co">#&gt; 3 3 3 6</span></a>
<a class="sourceLine" id="cb42-9" data-line-number="9"><span class="co">#&gt; 4 4 1 5</span></a>
<a class="sourceLine" id="cb42-10" data-line-number="10"><span class="co">#&gt; 5 5 4 9</span></a></code></pre></div>
</div>
<div id="exercises-2" class="section level3">
<h3>Exercises</h3>
<ol style="list-style-type: decimal">
<li><p>Write a function that selects all rows of <code>df</code> where <code>variable</code> is greater than its mean. Make the function more general by allowing the user to specify a function to use instead of <code>mean()</code> (e.g. <code>median()</code>).</p></li>
<li><p>Create a version of <code>mogrify()</code> where the first argument is <code>x</code>? What happens if you try to create a new variable called <code>x</code>?</p></li>
</ol>
</div>
</div>
<div id="non-standard-evaluation" class="section level2">
<h2>Non-standard evaluation</h2>
<p>In some situations you might want to eliminate the formula altogether, and allow the user to type expressions directly. I was once much enamoured with this approach (witness ggplot2, dplyr, …). However, I now think that it should be used sparingly because explict quoting with <code>~</code> leads to simpler code, and makes it more clear to the user that something special is going on.</p>
<p>That said, lazyeval does allow you to eliminate the <code>~</code> if you really want to. In this case, I recommend having both a NSE and SE version of the function. The SE version, which takes formuals, should have suffix <code>_</code>:</p>
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb43-1" data-line-number="1">sieve_ &lt;-<span class="st"> </span><span class="cf">function</span>(df, condition) {</a>
<a class="sourceLine" id="cb43-2" data-line-number="2">  rows &lt;-<span class="st"> </span><span class="kw">f_eval</span>(condition, df)</a>
<a class="sourceLine" id="cb43-3" data-line-number="3">  <span class="cf">if</span> (<span class="op">!</span><span class="kw">is.logical</span>(rows)) {</a>
<a class="sourceLine" id="cb43-4" data-line-number="4">    <span class="kw">stop</span>(<span class="st">&quot;`condition` must be logical.&quot;</span>, <span class="dt">call. =</span> <span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb43-5" data-line-number="5">  }</a>
<a class="sourceLine" id="cb43-6" data-line-number="6">  </a>
<a class="sourceLine" id="cb43-7" data-line-number="7">  rows[<span class="kw">is.na</span>(rows)] &lt;-<span class="st"> </span><span class="ot">FALSE</span></a>
<a class="sourceLine" id="cb43-8" data-line-number="8">  df[rows, , drop =<span class="st"> </span><span class="ot">FALSE</span>]</a>
<a class="sourceLine" id="cb43-9" data-line-number="9">}</a></code></pre></div>
<p>Then create the NSE version which doesn’t need the explicit formula. The key is the use of <code>f_capture()</code> which takes an unevaluated argument (a promise) and captures it as a formula:</p>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb44-1" data-line-number="1">sieve &lt;-<span class="st"> </span><span class="cf">function</span>(df, expr) {</a>
<a class="sourceLine" id="cb44-2" data-line-number="2">  <span class="kw">sieve_</span>(df, <span class="kw">f_capture</span>(expr))</a>
<a class="sourceLine" id="cb44-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb44-4" data-line-number="4"><span class="kw">sieve</span>(df, x <span class="op">==</span><span class="st"> </span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb44-5" data-line-number="5"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb44-6" data-line-number="6"><span class="co">#&gt; 1 1 2</span></a></code></pre></div>
<p>If you’re familiar with <code>substitute()</code> you might expect the same drawbacks to apply. However, <code>f_capture()</code> is smart enough to follow a chain of promises back to the original value, so, for example, this code works fine:</p>
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb45-1" data-line-number="1">scramble &lt;-<span class="st"> </span><span class="cf">function</span>(df) {</a>
<a class="sourceLine" id="cb45-2" data-line-number="2">  df[<span class="kw">sample</span>(<span class="kw">nrow</span>(df)), , drop =<span class="st"> </span><span class="ot">FALSE</span>]</a>
<a class="sourceLine" id="cb45-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb45-4" data-line-number="4">subscramble &lt;-<span class="st"> </span><span class="cf">function</span>(df, expr) {</a>
<a class="sourceLine" id="cb45-5" data-line-number="5">  <span class="kw">scramble</span>(<span class="kw">sieve</span>(df, expr))</a>
<a class="sourceLine" id="cb45-6" data-line-number="6">}</a>
<a class="sourceLine" id="cb45-7" data-line-number="7"><span class="kw">subscramble</span>(df, x <span class="op">&lt;</span><span class="st"> </span><span class="dv">4</span>)</a>
<a class="sourceLine" id="cb45-8" data-line-number="8"><span class="co">#&gt;   x y</span></a>
<a class="sourceLine" id="cb45-9" data-line-number="9"><span class="co">#&gt; 2 2 5</span></a>
<a class="sourceLine" id="cb45-10" data-line-number="10"><span class="co">#&gt; 1 1 2</span></a>
<a class="sourceLine" id="cb45-11" data-line-number="11"><span class="co">#&gt; 3 3 3</span></a></code></pre></div>
<div id="dot-dot-dot-1" class="section level3">
<h3>Dot-dot-dot</h3>
<p>If you want a <code>...</code> function that doesn’t require formulas, I recommend that the SE version take a list of arguments, and the NSE version uses <code>dots_capture()</code> to capture multiple arguments as a list of formulas.</p>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb46-1" data-line-number="1">mogrify_ &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="st">`</span><span class="dt">_df</span><span class="st">`</span>, args) {</a>
<a class="sourceLine" id="cb46-2" data-line-number="2">  args &lt;-<span class="st"> </span><span class="kw">as_f_list</span>(args)</a>
<a class="sourceLine" id="cb46-3" data-line-number="3">  </a>
<a class="sourceLine" id="cb46-4" data-line-number="4">  <span class="cf">for</span> (nm <span class="cf">in</span> <span class="kw">names</span>(args)) {</a>
<a class="sourceLine" id="cb46-5" data-line-number="5">    <span class="st">`</span><span class="dt">_df</span><span class="st">`</span>[[nm]] &lt;-<span class="st"> </span><span class="kw">f_eval</span>(args[[nm]], <span class="st">`</span><span class="dt">_df</span><span class="st">`</span>)</a>
<a class="sourceLine" id="cb46-6" data-line-number="6">  }</a>
<a class="sourceLine" id="cb46-7" data-line-number="7">  </a>
<a class="sourceLine" id="cb46-8" data-line-number="8">  <span class="st">`</span><span class="dt">_df</span><span class="st">`</span></a>
<a class="sourceLine" id="cb46-9" data-line-number="9">}</a>
<a class="sourceLine" id="cb46-10" data-line-number="10"></a>
<a class="sourceLine" id="cb46-11" data-line-number="11">mogrify &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="st">`</span><span class="dt">_df</span><span class="st">`</span>, ...) {</a>
<a class="sourceLine" id="cb46-12" data-line-number="12">  <span class="kw">mogrify_</span>(<span class="st">`</span><span class="dt">_df</span><span class="st">`</span>, <span class="kw">dots_capture</span>(...))</a>
<a class="sourceLine" id="cb46-13" data-line-number="13">}</a></code></pre></div>
</div>
<div id="exercises-3" class="section level3">
<h3>Exercises</h3>
<ol style="list-style-type: decimal">
<li>Recreate <code>subscramble()</code> using <code>base::subset()</code> instead of <code>sieve()</code>. Why does it fail?</li>
</ol>
</div>
</div>
<div id="metaprogramming" class="section level2">
<h2>Metaprogramming</h2>
<p>The final use of non-standard evaluation is to do metaprogramming. This is a catch-all term that encompasses any function that does computation on an unevaluated expression. You can learn about 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 time, the goal is to move all useful metaprogramming helper functions into this package, and discuss metaprogramming more here.</p>
</div>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>Currently neither ggplot2 nor dplyr actually use these tools since I’ve only just figured it out. But I’ll be working hard to make sure all my packages are consistent in the near future.<a href="#fnref1" class="footnote-back">↩</a></p></li>
</ol>
</div>



<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>