Debian Codegraph @

inst/doc/lazyeval-old.html

raw symbols SCIP
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
14 <meta name="date" content="2026-04-03" />
15
16 <title>Lazyeval: a new approach to NSE</title>
17
18 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
19 // be compatible with the behavior of Pandoc < 2.8).
20 document.addEventListener('DOMContentLoaded', function(e) {
21 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
22 var i, h, a;
23 for (i = 0; i < hs.length; i++) {
24 h = hs[i];
25 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
26 a = h.attributes;
27 while (a.length > 0) h.removeAttribute(a[0].name);
28 }
29 });
30 </script>
31
32 <style type="text/css">
33 code{white-space: pre-wrap;}
34 span.smallcaps{font-variant: small-caps;}
35 span.underline{text-decoration: underline;}
36 div.column{display: inline-block; vertical-align: top; width: 50%;}
37 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
38 ul.task-list{list-style: none;}
39 </style>
40
41
42
43 <style type="text/css">
44 code {
45 white-space: pre;
46 }
47 .sourceCode {
48 overflow: visible;
49 }
50 </style>
51 <style type="text/css" data-origin="pandoc">
52 html { -webkit-text-size-adjust: 100%; }
53 pre > code.sourceCode { white-space: pre; position: relative; }
54 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
55 pre > code.sourceCode > span:empty { height: 1.2em; }
56 .sourceCode { overflow: visible; }
57 code.sourceCode > span { color: inherit; text-decoration: inherit; }
58 div.sourceCode { margin: 1em 0; }
59 pre.sourceCode { margin: 0; }
60 @media screen {
61 div.sourceCode { overflow: auto; }
62 }
63 @media print {
64 pre > code.sourceCode { white-space: pre-wrap; }
65 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
66 }
67 pre.numberSource code
68 { counter-reset: source-line 0; }
69 pre.numberSource code > span
70 { position: relative; left: -4em; counter-increment: source-line; }
71 pre.numberSource code > span > a:first-child::before
72 { content: counter(source-line);
73 position: relative; left: -1em; text-align: right; vertical-align: baseline;
74 border: none; display: inline-block;
75 -webkit-touch-callout: none; -webkit-user-select: none;
76 -khtml-user-select: none; -moz-user-select: none;
77 -ms-user-select: none; user-select: none;
78 padding: 0 4px; width: 4em;
79 color: #aaaaaa;
80 }
81 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
82 div.sourceCode
83 { }
84 @media screen {
85 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
86 }
87 code span.al { color: #ff0000; font-weight: bold; }
88 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
89 code span.at { color: #7d9029; }
90 code span.bn { color: #40a070; }
91 code span.bu { color: #008000; }
92 code span.cf { color: #007020; font-weight: bold; }
93 code span.ch { color: #4070a0; }
94 code span.cn { color: #880000; }
95 code span.co { color: #60a0b0; font-style: italic; }
96 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
97 code span.do { color: #ba2121; font-style: italic; }
98 code span.dt { color: #902000; }
99 code span.dv { color: #40a070; }
100 code span.er { color: #ff0000; font-weight: bold; }
101 code span.ex { }
102 code span.fl { color: #40a070; }
103 code span.fu { color: #06287e; }
104 code span.im { color: #008000; font-weight: bold; }
105 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
106 code span.kw { color: #007020; font-weight: bold; }
107 code span.op { color: #666666; }
108 code span.ot { color: #007020; }
109 code span.pp { color: #bc7a00; }
110 code span.sc { color: #4070a0; }
111 code span.ss { color: #bb6688; }
112 code span.st { color: #4070a0; }
113 code span.va { color: #19177c; }
114 code span.vs { color: #4070a0; }
115 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
116 </style>
117 <script>
118 // apply pandoc div.sourceCode style to pre.sourceCode instead
119 (function() {
120 var sheets = document.styleSheets;
121 for (var i = 0; i < sheets.length; i++) {
122 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
123 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
124 var j = 0;
125 while (j < rules.length) {
126 var rule = rules[j];
127 // check if there is a div.sourceCode rule
128 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
129 j++;
130 continue;
131 }
132 var style = rule.style.cssText;
133 // check if color or background-color is set
134 if (rule.style.color === '' && rule.style.backgroundColor === '') {
135 j++;
136 continue;
137 }
138 // replace div.sourceCode by a pre.sourceCode rule
139 sheets[i].deleteRule(j);
140 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
141 }
142 }
143 })();
144 </script>
145
146
147
148
149 <style type="text/css">body {
150 background-color: #fff;
151 margin: 1em auto;
152 max-width: 700px;
153 overflow: visible;
154 padding-left: 2em;
155 padding-right: 2em;
156 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
157 font-size: 14px;
158 line-height: 1.35;
159 }
160 #TOC {
161 clear: both;
162 margin: 0 0 10px 10px;
163 padding: 4px;
164 width: 400px;
165 border: 1px solid #CCCCCC;
166 border-radius: 5px;
167 background-color: #f6f6f6;
168 font-size: 13px;
169 line-height: 1.3;
170 }
171 #TOC .toctitle {
172 font-weight: bold;
173 font-size: 15px;
174 margin-left: 5px;
175 }
176 #TOC ul {
177 padding-left: 40px;
178 margin-left: -1.5em;
179 margin-top: 5px;
180 margin-bottom: 5px;
181 }
182 #TOC ul ul {
183 margin-left: -2em;
184 }
185 #TOC li {
186 line-height: 16px;
187 }
188 table {
189 margin: 1em auto;
190 border-width: 1px;
191 border-color: #DDDDDD;
192 border-style: outset;
193 border-collapse: collapse;
194 }
195 table th {
196 border-width: 2px;
197 padding: 5px;
198 border-style: inset;
199 }
200 table td {
201 border-width: 1px;
202 border-style: inset;
203 line-height: 18px;
204 padding: 5px 5px;
205 }
206 table, table th, table td {
207 border-left-style: none;
208 border-right-style: none;
209 }
210 table thead, table tr.even {
211 background-color: #f7f7f7;
212 }
213 p {
214 margin: 0.5em 0;
215 }
216 blockquote {
217 background-color: #f6f6f6;
218 padding: 0.25em 0.75em;
219 }
220 hr {
221 border-style: solid;
222 border: none;
223 border-top: 1px solid #777;
224 margin: 28px 0;
225 }
226 dl {
227 margin-left: 0;
228 }
229 dl dd {
230 margin-bottom: 13px;
231 margin-left: 13px;
232 }
233 dl dt {
234 font-weight: bold;
235 }
236 ul {
237 margin-top: 0;
238 }
239 ul li {
240 list-style: circle outside;
241 }
242 ul ul {
243 margin-bottom: 0;
244 }
245 pre, code {
246 background-color: #f7f7f7;
247 border-radius: 3px;
248 color: #333;
249 white-space: pre-wrap;
250 }
251 pre {
252 border-radius: 3px;
253 margin: 5px 0px 10px 0px;
254 padding: 10px;
255 }
256 pre:not([class]) {
257 background-color: #f7f7f7;
258 }
259 code {
260 font-family: Consolas, Monaco, 'Courier New', monospace;
261 font-size: 85%;
262 }
263 p > code, li > code {
264 padding: 2px 0px;
265 }
266 div.figure {
267 text-align: center;
268 }
269 img {
270 background-color: #FFFFFF;
271 padding: 2px;
272 border: 1px solid #DDDDDD;
273 border-radius: 3px;
274 border: 1px solid #CCCCCC;
275 margin: 0 5px;
276 }
277 h1 {
278 margin-top: 0;
279 font-size: 35px;
280 line-height: 40px;
281 }
282 h2 {
283 border-bottom: 4px solid #f7f7f7;
284 padding-top: 10px;
285 padding-bottom: 2px;
286 font-size: 145%;
287 }
288 h3 {
289 border-bottom: 2px solid #f7f7f7;
290 padding-top: 10px;
291 font-size: 120%;
292 }
293 h4 {
294 border-bottom: 1px solid #f7f7f7;
295 margin-left: 8px;
296 font-size: 105%;
297 }
298 h5, h6 {
299 border-bottom: 1px solid #ccc;
300 font-size: 105%;
301 }
302 a {
303 color: #0033dd;
304 text-decoration: none;
305 }
306 a:hover {
307 color: #6666ff; }
308 a:visited {
309 color: #800080; }
310 a:visited:hover {
311 color: #BB00BB; }
312 a[href^="http:"] {
313 text-decoration: underline; }
314 a[href^="https:"] {
315 text-decoration: underline; }
316
317 code > span.kw { color: #555; font-weight: bold; }
318 code > span.dt { color: #902000; }
319 code > span.dv { color: #40a070; }
320 code > span.bn { color: #d14; }
321 code > span.fl { color: #d14; }
322 code > span.ch { color: #d14; }
323 code > span.st { color: #d14; }
324 code > span.co { color: #888888; font-style: italic; }
325 code > span.ot { color: #007020; }
326 code > span.al { color: #ff0000; font-weight: bold; }
327 code > span.fu { color: #900; font-weight: bold; }
328 code > span.er { color: #a61717; background-color: #e3d2d2; }
329 </style>
330
331
332
333
334 </head>
335
336 <body>
337
338
339
340
341 <h1 class="title toc-ignore">Lazyeval: a new approach to NSE</h1>
342 <h4 class="date">2026-04-03</h4>
343
344
345
346 <p>This document outlines my previous approach to non-standard
347 evaluation (NSE). You should avoid it unless you are working with an
348 older version of dplyr or tidyr.</p>
349 <p>There are three key ideas:</p>
350 <ul>
351 <li><p>Instead of using <code>substitute()</code>, use
352 <code>lazyeval::lazy()</code> to capture both expression and
353 environment. (Or use <code>lazyeval::lazy_dots(...)</code> to capture
354 promises in <code>...</code>)</p></li>
355 <li><p>Every function that uses NSE should have a standard evaluation
356 (SE) escape hatch that does the actual computation. The SE-function name
357 should end with <code>_</code>.</p></li>
358 <li><p>The SE-function has a flexible input specification to make it
359 easy for people to program with.</p></li>
360 </ul>
361 <div id="lazy" class="section level2">
362 <h2><code>lazy()</code></h2>
363 <p>The key tool that makes this approach possible is
364 <code>lazy()</code>, an equivalent to <code>substitute()</code> that
365 captures both expression and environment associated with a function
366 argument:</p>
367 <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">library</span>(lazyeval)</span>
368 <span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a>f <span class="ot">&lt;-</span> <span class="cf">function</span>(<span class="at">x =</span> a <span class="sc">-</span> b) {</span>
369 <span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a> <span class="fu">lazy</span>(x)</span>
370 <span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a>}</span>
371 <span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="fu">f</span>()</span>
372 <span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="co">#&gt; &lt;lazy&gt;</span></span>
373 <span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="co">#&gt; expr: a - b</span></span>
374 <span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="co">#&gt; env: &lt;environment: 0x8b36f4f58&gt;</span></span>
375 <span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="fu">f</span>(a <span class="sc">+</span> b)</span>
376 <span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="co">#&gt; &lt;lazy&gt;</span></span>
377 <span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="co">#&gt; expr: a + b</span></span>
378 <span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a><span class="co">#&gt; env: &lt;environment: R_GlobalEnv&gt;</span></span></code></pre></div>
379 <p>As a complement to <code>eval()</code>, the lazy package provides
380 <code>lazy_eval()</code> that uses the environment associated with the
381 lazy object:</p>
382 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>a <span class="ot">&lt;-</span> <span class="dv">10</span></span>
383 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a>b <span class="ot">&lt;-</span> <span class="dv">1</span></span>
384 <span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="fu">lazy_eval</span>(<span class="fu">f</span>())</span>
385 <span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co">#&gt; [1] 9</span></span>
386 <span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="fu">lazy_eval</span>(<span class="fu">f</span>(a <span class="sc">+</span> b))</span>
387 <span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co">#&gt; [1] 11</span></span></code></pre></div>
388 <p>The second argument to lazy eval is a list or data frame where names
389 should be looked up first:</p>
390 <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">lazy_eval</span>(<span class="fu">f</span>(), <span class="fu">list</span>(<span class="at">a =</span> <span class="dv">1</span>))</span>
391 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="co">#&gt; [1] 0</span></span></code></pre></div>
392 <p><code>lazy_eval()</code> also works with formulas, since they contain
393 the same information as a lazy object: an expression (only the RHS is
394 used by convention) and an environment:</p>
395 <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">lazy_eval</span>(<span class="sc">~</span> a <span class="sc">+</span> b)</span>
396 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="co">#&gt; [1] 11</span></span>
397 <span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a>h <span class="ot">&lt;-</span> <span class="cf">function</span>(i) {</span>
398 <span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a> <span class="sc">~</span> <span class="dv">10</span> <span class="sc">+</span> i</span>
399 <span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a>}</span>
400 <span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="fu">lazy_eval</span>(<span class="fu">h</span>(<span class="dv">1</span>))</span>
401 <span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#&gt; [1] 11</span></span></code></pre></div>
402 </div>
403 <div id="standard-evaluation" class="section level2">
404 <h2>Standard evaluation</h2>
405 <p>Whenever we need a function that does non-standard evaluation, always
406 write the standard evaluation version first. For example, let’s
407 implement our own version of <code>subset()</code>:</p>
408 <div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>subset2_ <span class="ot">&lt;-</span> <span class="cf">function</span>(df, condition) {</span>
409 <span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a> r <span class="ot">&lt;-</span> <span class="fu">lazy_eval</span>(condition, df)</span>
410 <span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a> r <span class="ot">&lt;-</span> r <span class="sc">&amp;</span> <span class="sc">!</span><span class="fu">is.na</span>(r)</span>
411 <span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a> df[r, , drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
412 <span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a>} </span>
413 <span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a></span>
414 <span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="fu">subset2_</span>(mtcars, <span class="fu">lazy</span>(mpg <span class="sc">&gt;</span> <span class="dv">31</span>))</span>
415 <span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
416 <span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
417 <span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span></code></pre></div>
418 <p><code>lazy_eval()</code> will always coerce it’s first argument into
419 a lazy object, so a variety of specifications will work:</p>
420 <div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="fu">subset2_</span>(mtcars, <span class="sc">~</span>mpg <span class="sc">&gt;</span> <span class="dv">31</span>)</span>
421 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
422 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
423 <span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span>
424 <span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="fu">subset2_</span>(mtcars, <span class="fu">quote</span>(mpg <span class="sc">&gt;</span> <span class="dv">31</span>))</span>
425 <span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
426 <span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
427 <span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span>
428 <span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="fu">subset2_</span>(mtcars, <span class="st">&quot;mpg &gt; 31&quot;</span>)</span>
429 <span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
430 <span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
431 <span id="cb6-12"><a href="#cb6-12" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span></code></pre></div>
432 <p>Note that quoted called and strings don’t have environments
433 associated with them, so <code>as.lazy()</code> defaults to using
434 <code>baseenv()</code>. This will work if the expression is
435 self-contained (i.e. doesn’t contain any references to variables in the
436 local environment), and will otherwise fail quickly and robustly.</p>
437 </div>
438 <div id="non-standard-evaluation" class="section level2">
439 <h2>Non-standard evaluation</h2>
440 <p>With the SE version in hand, writing the NSE version is easy. We just
441 use <code>lazy()</code> to capture the unevaluated expression and
442 corresponding environment:</p>
443 <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>subset2 <span class="ot">&lt;-</span> <span class="cf">function</span>(df, condition) {</span>
444 <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a> <span class="fu">subset2_</span>(df, <span class="fu">lazy</span>(condition))</span>
445 <span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a>}</span>
446 <span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="fu">subset2</span>(mtcars, mpg <span class="sc">&gt;</span> <span class="dv">31</span>)</span>
447 <span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
448 <span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
449 <span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span></code></pre></div>
450 <p>This standard evaluation escape hatch is very important because it
451 allows us to implement different NSE approaches. For example, we could
452 create a subsetting function that finds all rows where a variable is
453 above a threshold:</p>
454 <div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>above_threshold <span class="ot">&lt;-</span> <span class="cf">function</span>(df, var, threshold) {</span>
455 <span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> cond <span class="ot">&lt;-</span> <span class="fu">interp</span>(<span class="sc">~</span> var <span class="sc">&gt;</span> x, <span class="at">var =</span> <span class="fu">lazy</span>(var), <span class="at">x =</span> threshold)</span>
456 <span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a> <span class="fu">subset2_</span>(df, cond)</span>
457 <span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a>}</span>
458 <span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="fu">above_threshold</span>(mtcars, mpg, <span class="dv">31</span>)</span>
459 <span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
460 <span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
461 <span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span></code></pre></div>
462 <p>Here we’re using <code>interp()</code> to modify a formula. We use
463 the value of <code>threshold</code> and the expression in by
464 <code>var</code>.</p>
465 </div>
466 <div id="scoping" class="section level2">
467 <h2>Scoping</h2>
468 <p>Because <code>lazy()</code> captures the environment associated with
469 the function argument, we automatically avoid a subtle scoping bug
470 present in <code>subset()</code>:</p>
471 <div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="dv">31</span></span>
472 <span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>f1 <span class="ot">&lt;-</span> <span class="cf">function</span>(...) {</span>
473 <span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> x <span class="ot">&lt;-</span> <span class="dv">30</span></span>
474 <span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a> <span class="fu">subset</span>(mtcars, ...)</span>
475 <span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a>}</span>
476 <span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co"># Uses 30 instead of 31</span></span>
477 <span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="fu">f1</span>(mpg <span class="sc">&gt;</span> x)</span>
478 <span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
479 <span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
480 <span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a><span class="co">#&gt; 19 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2</span></span>
481 <span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span>
482 <span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a><span class="co">#&gt; 28 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2</span></span>
483 <span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a></span>
484 <span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a>f2 <span class="ot">&lt;-</span> <span class="cf">function</span>(...) {</span>
485 <span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a> x <span class="ot">&lt;-</span> <span class="dv">30</span></span>
486 <span id="cb9-16"><a href="#cb9-16" tabindex="-1"></a> <span class="fu">subset2</span>(mtcars, ...)</span>
487 <span id="cb9-17"><a href="#cb9-17" tabindex="-1"></a>}</span>
488 <span id="cb9-18"><a href="#cb9-18" tabindex="-1"></a><span class="co"># Correctly uses 31</span></span>
489 <span id="cb9-19"><a href="#cb9-19" tabindex="-1"></a><span class="fu">f2</span>(mpg <span class="sc">&gt;</span> x)</span>
490 <span id="cb9-20"><a href="#cb9-20" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
491 <span id="cb9-21"><a href="#cb9-21" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
492 <span id="cb9-22"><a href="#cb9-22" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span></code></pre></div>
493 <p><code>lazy()</code> has another advantage over
494 <code>substitute()</code> - by default, it follows promises across
495 function invocations. This simplifies the casual use of NSE.</p>
496 <div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="dv">31</span></span>
497 <span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a>g1 <span class="ot">&lt;-</span> <span class="cf">function</span>(comp) {</span>
498 <span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a> x <span class="ot">&lt;-</span> <span class="dv">30</span></span>
499 <span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a> <span class="fu">subset</span>(mtcars, comp)</span>
500 <span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a>}</span>
501 <span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="fu">g1</span>(mpg <span class="sc">&gt;</span> x)</span>
502 <span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#&gt; Error: object &#39;mpg&#39; not found</span></span></code></pre></div>
503 <div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>g2 <span class="ot">&lt;-</span> <span class="cf">function</span>(comp) {</span>
504 <span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a> x <span class="ot">&lt;-</span> <span class="dv">30</span></span>
505 <span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a> <span class="fu">subset2</span>(mtcars, comp)</span>
506 <span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a>}</span>
507 <span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="fu">g2</span>(mpg <span class="sc">&gt;</span> x)</span>
508 <span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a><span class="co">#&gt; mpg cyl disp hp drat wt qsec vs am gear carb</span></span>
509 <span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co">#&gt; 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</span></span>
510 <span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a><span class="co">#&gt; 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</span></span></code></pre></div>
511 <p>Note that <code>g2()</code> doesn’t have a standard-evaluation escape
512 hatch, so it’s not suitable for programming with in the same way that
513 <code>subset2_()</code> is.</p>
514 </div>
515 <div id="chained-promises" class="section level2">
516 <h2>Chained promises</h2>
517 <p>Take the following example:</p>
518 <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">library</span>(lazyeval)</span>
519 <span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a>f1 <span class="ot">&lt;-</span> <span class="cf">function</span>(x) <span class="fu">lazy</span>(x)</span>
520 <span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a>g1 <span class="ot">&lt;-</span> <span class="cf">function</span>(y) <span class="fu">f1</span>(y)</span>
521 <span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a></span>
522 <span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="fu">g1</span>(a <span class="sc">+</span> b)</span>
523 <span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#&gt; &lt;lazy&gt;</span></span>
524 <span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#&gt; expr: a + b</span></span>
525 <span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="co">#&gt; env: &lt;environment: R_GlobalEnv&gt;</span></span></code></pre></div>
526 <p><code>lazy()</code> returns <code>a + b</code> because it always
527 tries to find the top-level promise.</p>
528 <p>In this case the process looks like this:</p>
529 <ol style="list-style-type: decimal">
530 <li>Find the object that <code>x</code> is bound to.</li>
531 <li>It’s a promise, so find the expr it’s bound to (<code>y</code>, a
532 symbol) and the environment in which it should be evaluated (the
533 environment of <code>g()</code>).</li>
534 <li>Since <code>x</code> is bound to a symbol, look up its value: it’s
535 bound to a promise.</li>
536 <li>That promise has expression <code>a + b</code> and should be
537 evaluated in the global environment.</li>
538 <li>The expression is not a symbol, so stop.</li>
539 </ol>
540 <p>Occasionally, you want to avoid this recursive behaviour, so you can
541 use <code>follow_symbol = FALSE</code>:</p>
542 <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>f2 <span class="ot">&lt;-</span> <span class="cf">function</span>(x) <span class="fu">lazy</span>(x, <span class="at">.follow_symbols =</span> <span class="cn">FALSE</span>)</span>
543 <span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a>g2 <span class="ot">&lt;-</span> <span class="cf">function</span>(y) <span class="fu">f2</span>(y)</span>
544 <span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a></span>
545 <span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="fu">g2</span>(a <span class="sc">+</span> b)</span>
546 <span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#&gt; &lt;lazy&gt;</span></span>
547 <span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co">#&gt; expr: x</span></span>
548 <span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a><span class="co">#&gt; env: &lt;environment: 0x8b0915d58&gt;</span></span></code></pre></div>
549 <p>Either way, if you evaluate the lazy expression you’ll get the same
550 result:</p>
551 <div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>a <span class="ot">&lt;-</span> <span class="dv">10</span></span>
552 <span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a>b <span class="ot">&lt;-</span> <span class="dv">1</span></span>
553 <span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a></span>
554 <span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="fu">lazy_eval</span>(<span class="fu">g1</span>(a <span class="sc">+</span> b))</span>
555 <span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#&gt; [1] 11</span></span>
556 <span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="fu">lazy_eval</span>(<span class="fu">g2</span>(a <span class="sc">+</span> b))</span>
557 <span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#&gt; [1] 11</span></span></code></pre></div>
558 <p>Note that the resolution of chained promises only works with
559 unevaluated objects. This is because R deletes the information about the
560 environment associated with a promise when it has been forced, so that
561 the garbage collector is allowed to remove the environment from memory
562 in case it is no longer used. <code>lazy()</code> will fail with an
563 error in such situations.</p>
564 <div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>var <span class="ot">&lt;-</span> <span class="dv">0</span></span>
565 <span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a></span>
566 <span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a>f3 <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
567 <span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a> <span class="fu">force</span>(x)</span>
568 <span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a> <span class="fu">lazy</span>(x)</span>
569 <span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a>}</span>
570 <span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a></span>
571 <span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a><span class="fu">f3</span>(var)</span>
572 <span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a><span class="co">#&gt; Error in `lazy()`:</span></span>
573 <span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a><span class="co">#&gt; ! Promise has already been forced</span></span></code></pre></div>
574 </div>
575
576
577
578 <!-- code folding -->
579
580
581 <!-- dynamically load mathjax for compatibility with self-contained -->
582 <script>
583 (function () {
584 var script = document.createElement("script");
585 script.type = "text/javascript";
586 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
587 document.getElementsByTagName("head")[0].appendChild(script);
588 })();
589 </script>
590
591 </body>
592 </html>