Debian Codegraph @

inst/doc/glue.html

raw
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
15 <title>Introduction to glue</title>
16
17 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
18 // be compatible with the behavior of Pandoc < 2.8).
19 document.addEventListener('DOMContentLoaded', function(e) {
20 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
21 var i, h, a;
22 for (i = 0; i < hs.length; i++) {
23 h = hs[i];
24 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
25 a = h.attributes;
26 while (a.length > 0) h.removeAttribute(a[0].name);
27 }
28 });
29 </script>
30
31 <style type="text/css">
32 code{white-space: pre-wrap;}
33 span.smallcaps{font-variant: small-caps;}
34 span.underline{text-decoration: underline;}
35 div.column{display: inline-block; vertical-align: top; width: 50%;}
36 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
37 ul.task-list{list-style: none;}
38 </style>
39
40
41
42 <style type="text/css">
43 code {
44 white-space: pre;
45 }
46 .sourceCode {
47 overflow: visible;
48 }
49 </style>
50 <style type="text/css" data-origin="pandoc">
51 html { -webkit-text-size-adjust: 100%; }
52 pre > code.sourceCode { white-space: pre; position: relative; }
53 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
54 pre > code.sourceCode > span:empty { height: 1.2em; }
55 .sourceCode { overflow: visible; }
56 code.sourceCode > span { color: inherit; text-decoration: inherit; }
57 div.sourceCode { margin: 1em 0; }
58 pre.sourceCode { margin: 0; }
59 @media screen {
60 div.sourceCode { overflow: auto; }
61 }
62 @media print {
63 pre > code.sourceCode { white-space: pre-wrap; }
64 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
65 }
66 pre.numberSource code
67 { counter-reset: source-line 0; }
68 pre.numberSource code > span
69 { position: relative; left: -4em; counter-increment: source-line; }
70 pre.numberSource code > span > a:first-child::before
71 { content: counter(source-line);
72 position: relative; left: -1em; text-align: right; vertical-align: baseline;
73 border: none; display: inline-block;
74 -webkit-touch-callout: none; -webkit-user-select: none;
75 -khtml-user-select: none; -moz-user-select: none;
76 -ms-user-select: none; user-select: none;
77 padding: 0 4px; width: 4em;
78 color: #aaaaaa;
79 }
80 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
81 div.sourceCode
82 { }
83 @media screen {
84 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
85 }
86 code span.al { color: #ff0000; font-weight: bold; }
87 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
88 code span.at { color: #7d9029; }
89 code span.bn { color: #40a070; }
90 code span.bu { color: #008000; }
91 code span.cf { color: #007020; font-weight: bold; }
92 code span.ch { color: #4070a0; }
93 code span.cn { color: #880000; }
94 code span.co { color: #60a0b0; font-style: italic; }
95 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
96 code span.do { color: #ba2121; font-style: italic; }
97 code span.dt { color: #902000; }
98 code span.dv { color: #40a070; }
99 code span.er { color: #ff0000; font-weight: bold; }
100 code span.ex { }
101 code span.fl { color: #40a070; }
102 code span.fu { color: #06287e; }
103 code span.im { color: #008000; font-weight: bold; }
104 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
105 code span.kw { color: #007020; font-weight: bold; }
106 code span.op { color: #666666; }
107 code span.ot { color: #007020; }
108 code span.pp { color: #bc7a00; }
109 code span.sc { color: #4070a0; }
110 code span.ss { color: #bb6688; }
111 code span.st { color: #4070a0; }
112 code span.va { color: #19177c; }
113 code span.vs { color: #4070a0; }
114 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
115 </style>
116 <script>
117 // apply pandoc div.sourceCode style to pre.sourceCode instead
118 (function() {
119 var sheets = document.styleSheets;
120 for (var i = 0; i < sheets.length; i++) {
121 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
122 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
123 var j = 0;
124 while (j < rules.length) {
125 var rule = rules[j];
126 // check if there is a div.sourceCode rule
127 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
128 j++;
129 continue;
130 }
131 var style = rule.style.cssText;
132 // check if color or background-color is set
133 if (rule.style.color === '' && rule.style.backgroundColor === '') {
134 j++;
135 continue;
136 }
137 // replace div.sourceCode by a pre.sourceCode rule
138 sheets[i].deleteRule(j);
139 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
140 }
141 }
142 })();
143 </script>
144
145
146
147
148 <style type="text/css">body {
149 background-color: #fff;
150 margin: 1em auto;
151 max-width: 700px;
152 overflow: visible;
153 padding-left: 2em;
154 padding-right: 2em;
155 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
156 font-size: 14px;
157 line-height: 1.35;
158 }
159 #TOC {
160 clear: both;
161 margin: 0 0 10px 10px;
162 padding: 4px;
163 width: 400px;
164 border: 1px solid #CCCCCC;
165 border-radius: 5px;
166 background-color: #f6f6f6;
167 font-size: 13px;
168 line-height: 1.3;
169 }
170 #TOC .toctitle {
171 font-weight: bold;
172 font-size: 15px;
173 margin-left: 5px;
174 }
175 #TOC ul {
176 padding-left: 40px;
177 margin-left: -1.5em;
178 margin-top: 5px;
179 margin-bottom: 5px;
180 }
181 #TOC ul ul {
182 margin-left: -2em;
183 }
184 #TOC li {
185 line-height: 16px;
186 }
187 table {
188 margin: 1em auto;
189 border-width: 1px;
190 border-color: #DDDDDD;
191 border-style: outset;
192 border-collapse: collapse;
193 }
194 table th {
195 border-width: 2px;
196 padding: 5px;
197 border-style: inset;
198 }
199 table td {
200 border-width: 1px;
201 border-style: inset;
202 line-height: 18px;
203 padding: 5px 5px;
204 }
205 table, table th, table td {
206 border-left-style: none;
207 border-right-style: none;
208 }
209 table thead, table tr.even {
210 background-color: #f7f7f7;
211 }
212 p {
213 margin: 0.5em 0;
214 }
215 blockquote {
216 background-color: #f6f6f6;
217 padding: 0.25em 0.75em;
218 }
219 hr {
220 border-style: solid;
221 border: none;
222 border-top: 1px solid #777;
223 margin: 28px 0;
224 }
225 dl {
226 margin-left: 0;
227 }
228 dl dd {
229 margin-bottom: 13px;
230 margin-left: 13px;
231 }
232 dl dt {
233 font-weight: bold;
234 }
235 ul {
236 margin-top: 0;
237 }
238 ul li {
239 list-style: circle outside;
240 }
241 ul ul {
242 margin-bottom: 0;
243 }
244 pre, code {
245 background-color: #f7f7f7;
246 border-radius: 3px;
247 color: #333;
248 white-space: pre-wrap;
249 }
250 pre {
251 border-radius: 3px;
252 margin: 5px 0px 10px 0px;
253 padding: 10px;
254 }
255 pre:not([class]) {
256 background-color: #f7f7f7;
257 }
258 code {
259 font-family: Consolas, Monaco, 'Courier New', monospace;
260 font-size: 85%;
261 }
262 p > code, li > code {
263 padding: 2px 0px;
264 }
265 div.figure {
266 text-align: center;
267 }
268 img {
269 background-color: #FFFFFF;
270 padding: 2px;
271 border: 1px solid #DDDDDD;
272 border-radius: 3px;
273 border: 1px solid #CCCCCC;
274 margin: 0 5px;
275 }
276 h1 {
277 margin-top: 0;
278 font-size: 35px;
279 line-height: 40px;
280 }
281 h2 {
282 border-bottom: 4px solid #f7f7f7;
283 padding-top: 10px;
284 padding-bottom: 2px;
285 font-size: 145%;
286 }
287 h3 {
288 border-bottom: 2px solid #f7f7f7;
289 padding-top: 10px;
290 font-size: 120%;
291 }
292 h4 {
293 border-bottom: 1px solid #f7f7f7;
294 margin-left: 8px;
295 font-size: 105%;
296 }
297 h5, h6 {
298 border-bottom: 1px solid #ccc;
299 font-size: 105%;
300 }
301 a {
302 color: #0033dd;
303 text-decoration: none;
304 }
305 a:hover {
306 color: #6666ff; }
307 a:visited {
308 color: #800080; }
309 a:visited:hover {
310 color: #BB00BB; }
311 a[href^="http:"] {
312 text-decoration: underline; }
313 a[href^="https:"] {
314 text-decoration: underline; }
315
316 code > span.kw { color: #555; font-weight: bold; }
317 code > span.dt { color: #902000; }
318 code > span.dv { color: #40a070; }
319 code > span.bn { color: #d14; }
320 code > span.fl { color: #d14; }
321 code > span.ch { color: #d14; }
322 code > span.st { color: #d14; }
323 code > span.co { color: #888888; font-style: italic; }
324 code > span.ot { color: #007020; }
325 code > span.al { color: #ff0000; font-weight: bold; }
326 code > span.fu { color: #900; font-weight: bold; }
327 code > span.er { color: #a61717; background-color: #e3d2d2; }
328 </style>
329
330
331
332
333 </head>
334
335 <body>
336
337
338
339
340 <h1 class="title toc-ignore">Introduction to glue</h1>
341
342
343
344 <p>The glue package contains functions for string interpolation: gluing
345 together character strings and R code.</p>
346 <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>(glue)</span></code></pre></div>
347 <div id="gluing-and-interpolating" class="section level2">
348 <h2>Gluing and interpolating</h2>
349 <p><code>glue()</code> can be used to glue together pieces of text:</p>
350 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">&quot;glue &quot;</span>, <span class="st">&quot;some &quot;</span>, <span class="st">&quot;text &quot;</span>, <span class="st">&quot;together&quot;</span>)</span>
351 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="co">#&gt; glue some text together</span></span></code></pre></div>
352 <p>But glue’s real power comes with <code>{}</code>: anything inside of
353 <code>{}</code> is evaluated and pasted into the string. This makes it
354 easy to interpolate variables:</p>
355 <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>name <span class="ot">&lt;-</span> <span class="st">&quot;glue&quot;</span></span>
356 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">&quot;We are learning how to use the {name} R package.&quot;</span>)</span>
357 <span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#&gt; We are learning how to use the glue R package.</span></span></code></pre></div>
358 <p>As well as more complex expressions:</p>
359 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>release_date <span class="ot">&lt;-</span> <span class="fu">as.Date</span>(<span class="st">&quot;2017-06-13&quot;</span>)</span>
360 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">&quot;Release was on a {format(release_date, &#39;%A&#39;)}.&quot;</span>)</span>
361 <span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#&gt; Release was on a Tuesday.</span></span></code></pre></div>
362 </div>
363 <div id="control-of-line-breaks" class="section level2">
364 <h2>Control of line breaks</h2>
365 <p><code>glue()</code> honors the line breaks in its input:</p>
366 <div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">&quot;</span></span>
367 <span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="st"> A formatted string</span></span>
368 <span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="st"> Can have multiple lines</span></span>
369 <span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="st"> with additional indentation preserved</span></span>
370 <span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="st"> &quot;</span></span>
371 <span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a>)</span>
372 <span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#&gt; A formatted string</span></span>
373 <span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#&gt; Can have multiple lines</span></span>
374 <span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#&gt; with additional indentation preserved</span></span></code></pre></div>
375 <p>The example above demonstrates some other important facts about the
376 pre-processing of the template string:</p>
377 <ul>
378 <li>An empty first or last line is automatically trimmed.</li>
379 <li>Leading whitespace that is common across all lines is trimmed.</li>
380 </ul>
381 <p>The elimination of common leading whitespace is advantageous, because
382 you aren’t forced to choose between indenting your code normally and
383 getting the output you actually want. This is easier to appreciate when
384 you have <code>glue()</code> inside a function body (this example also
385 shows an alternative way of styling the end of a <code>glue()</code>
386 call):</p>
387 <div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>foo <span class="ot">&lt;-</span> <span class="cf">function</span>() {</span>
388 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a> <span class="fu">glue</span>(<span class="st">&quot;</span></span>
389 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="st"> A formatted string</span></span>
390 <span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="st"> Can have multiple lines</span></span>
391 <span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="st"> with additional indentation preserved&quot;</span>)</span>
392 <span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a>}</span>
393 <span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="fu">foo</span>()</span>
394 <span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#&gt; A formatted string</span></span>
395 <span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="co">#&gt; Can have multiple lines</span></span>
396 <span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#&gt; with additional indentation preserved</span></span></code></pre></div>
397 <p>On the other hand, what if you don’t want a line break in the output,
398 but you also like to limit the length of lines in your source code to,
399 e.g., 80 characters? The first option is to use <code>\\</code> to break
400 the template string into multiple lines, without getting line breaks in
401 the output:</p>
402 <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>release_date <span class="ot">&lt;-</span> <span class="fu">as.Date</span>(<span class="st">&quot;2017-06-13&quot;</span>)</span>
403 <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">&quot;</span></span>
404 <span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="st"> The first version of the glue package was released on </span><span class="sc">\\</span></span>
405 <span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="st"> a {format(release_date, &#39;%A&#39;)}.&quot;</span>)</span>
406 <span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#&gt; The first version of the glue package was released on a Tuesday.</span></span></code></pre></div>
407 <p>This comes up fairly often when an expression to evaluate inside
408 <code>{}</code> takes up more characters than its result,
409 i.e. <code>format(release_date, &#39;%A&#39;)</code> versus
410 <code>Tuesday</code>. A second way to achieve the same result is to
411 break the template into individual pieces, which are then
412 concatenated.</p>
413 <div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="fu">glue</span>(</span>
414 <span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> <span class="st">&quot;The first version of the glue package was released on &quot;</span>,</span>
415 <span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a> <span class="st">&quot;a {format(release_date, &#39;%A&#39;)}.&quot;</span></span>
416 <span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a>)</span>
417 <span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co">#&gt; The first version of the glue package was released on a Tuesday.</span></span></code></pre></div>
418 <p>If you want an explicit newline at the start or end, include an extra
419 empty line.</p>
420 <div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="co"># no leading or trailing newline</span></span>
421 <span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">glue</span>(<span class="st">&quot;</span></span>
422 <span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="st"> blah</span></span>
423 <span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="st"> &quot;</span>)</span>
424 <span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="fu">unclass</span>(x)</span>
425 <span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co">#&gt; [1] &quot;blah&quot;</span></span>
426 <span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a></span>
427 <span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co"># both a leading and trailing newline</span></span>
428 <span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a>y <span class="ot">&lt;-</span> <span class="fu">glue</span>(<span class="st">&quot;</span></span>
429 <span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a></span>
430 <span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a><span class="st"> blah</span></span>
431 <span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a></span>
432 <span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a><span class="st"> &quot;</span>)</span>
433 <span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a><span class="fu">unclass</span>(y)</span>
434 <span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a><span class="co">#&gt; [1] &quot;\nblah\n&quot;</span></span></code></pre></div>
435 <p>We use <code>unclass()</code> above to make it easier to see the
436 absence and presence of the newlines, i.e. to reveal the literal
437 <code>\n</code> escape sequences. <code>glue()</code> and friends
438 generally return a glue object, which is a character vector with the S3
439 class <code>&quot;glue&quot;</code>. The <code>&quot;glue&quot;</code> class exists
440 primarily for the sake of a print method, which displays the natural
441 formatted result of a glue string. Most of the time this is
442 <em>exactly</em> what the user wants to see. The example above happens
443 to be an exception, where we really do want to see the underlying string
444 representation.</p>
445 <p>Here’s another example to drive home the difference between printing
446 a glue object and looking at its string representation.
447 <code>as.character()</code> is a another way to do this that is arguably
448 more expressive.</p>
449 <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="fu">glue</span>(<span class="st">&#39;</span></span>
450 <span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="st"> abc</span></span>
451 <span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="st"> &quot; }</span></span>
452 <span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a></span>
453 <span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="st"> xyz&#39;</span>)</span>
454 <span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="fu">class</span>(x)</span>
455 <span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#&gt; [1] &quot;glue&quot; &quot;character&quot;</span></span>
456 <span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a></span>
457 <span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a>x</span>
458 <span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="co">#&gt; abc</span></span>
459 <span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co">#&gt; &quot; }</span></span>
460 <span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a><span class="co">#&gt; </span></span>
461 <span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a><span class="co">#&gt; xyz</span></span>
462 <span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a><span class="fu">unclass</span>(x)</span>
463 <span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a><span class="co">#&gt; [1] &quot;abc\n\&quot;\t}\n\nxyz&quot;</span></span>
464 <span id="cb10-16"><a href="#cb10-16" tabindex="-1"></a><span class="fu">as.character</span>(x)</span>
465 <span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a><span class="co">#&gt; [1] &quot;abc\n\&quot;\t}\n\nxyz&quot;</span></span></code></pre></div>
466 </div>
467 <div id="delimiters" class="section level2">
468 <h2>Delimiters</h2>
469 <p>By default, code to be evaluated goes inside <code>{}</code> in a
470 glue string. If want a literal curly brace in your string, double
471 it:</p>
472 <div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">&quot;The name of the package is {name}, not {{name}}.&quot;</span>)</span>
473 <span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="co">#&gt; The name of the package is glue, not {name}.</span></span></code></pre></div>
474 <p>Sometimes it’s just more convenient to use different delimiters
475 altogether, especially if the template text comes from elsewhere or is
476 subject to external requirements. Consider this example where we want to
477 interpolate the function name into a code snippet that defines a
478 function:</p>
479 <div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>fn_def <span class="ot">&lt;-</span> <span class="st">&quot;</span></span>
480 <span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="st"> &lt;&lt;NAME&gt;&gt; &lt;- function(x) {</span></span>
481 <span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="st"> # imagine a function body here</span></span>
482 <span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="st"> }&quot;</span></span>
483 <span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="fu">glue</span>(fn_def, <span class="at">NAME =</span> <span class="st">&quot;my_function&quot;</span>, <span class="at">.open =</span> <span class="st">&quot;&lt;&lt;&quot;</span>, <span class="at">.close =</span> <span class="st">&quot;&gt;&gt;&quot;</span>)</span>
484 <span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#&gt; my_function &lt;- function(x) {</span></span>
485 <span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#&gt; # imagine a function body here</span></span>
486 <span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="co">#&gt; }</span></span></code></pre></div>
487 <p>In this glue string, <code>{</code> and <code>}</code> have very
488 special meaning. If we forced ourselves to double them, suddenly it
489 doesn’t look like normal R code anymore. Using alternative delimiters is
490 a nice option in cases like this.</p>
491 </div>
492 <div id="where-glue-looks-for-values" class="section level2">
493 <h2>Where glue looks for values</h2>
494 <p>By default, <code>glue()</code> evaluates the code inside
495 <code>{}</code> in the caller environment:</p>
496 <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a><span class="fu">glue</span>(..., <span class="at">.envir =</span> <span class="fu">parent.frame</span>())</span></code></pre></div>
497 <p>So, for a top-level <code>glue()</code> call, that means the global
498 environment.</p>
499 <div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="st">&quot;the caller environment&quot;</span></span>
500 <span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">&quot;By default, `glue()` evaluates code in {x}.&quot;</span>)</span>
501 <span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="co">#&gt; By default, `glue()` evaluates code in the caller environment.</span></span></code></pre></div>
502 <p>But you can provide more narrowly scoped values by passing them to
503 <code>glue()</code> in <code>name = value</code> form:</p>
504 <div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="st">&quot;the local environment&quot;</span></span>
505 <span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="fu">glue</span>(</span>
506 <span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> <span class="st">&quot;`glue()` can access values from {x} or from {y}. {z}&quot;</span>,</span>
507 <span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a> <span class="at">y =</span> <span class="st">&quot;named arguments&quot;</span>,</span>
508 <span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a> <span class="at">z =</span> <span class="st">&quot;Woo!&quot;</span></span>
509 <span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a>)</span>
510 <span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a><span class="co">#&gt; `glue()` can access values from the local environment or from named arguments. Woo!</span></span></code></pre></div>
511 <p>If the relevant data lives in a data frame (or list or environment),
512 use <code>glue_data()</code> instead:</p>
513 <div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>mini_mtcars <span class="ot">&lt;-</span> <span class="fu">head</span>(<span class="fu">cbind</span>(<span class="at">model =</span> <span class="fu">rownames</span>(mtcars), mtcars))</span>
514 <span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a><span class="fu">rownames</span>(mini_mtcars) <span class="ot">&lt;-</span> <span class="cn">NULL</span></span>
515 <span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a><span class="fu">glue_data</span>(mini_mtcars, <span class="st">&quot;{model} has {hp} hp.&quot;</span>)</span>
516 <span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a><span class="co">#&gt; Mazda RX4 has 110 hp.</span></span>
517 <span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="co">#&gt; Mazda RX4 Wag has 110 hp.</span></span>
518 <span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="co">#&gt; Datsun 710 has 93 hp.</span></span>
519 <span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a><span class="co">#&gt; Hornet 4 Drive has 110 hp.</span></span>
520 <span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="co">#&gt; Hornet Sportabout has 175 hp.</span></span>
521 <span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a><span class="co">#&gt; Valiant has 105 hp.</span></span></code></pre></div>
522 <p><code>glue_data()</code> is very natural to use with the pipe:</p>
523 <div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a>mini_mtcars <span class="sc">|&gt;</span></span>
524 <span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a> <span class="fu">glue_data</span>(<span class="st">&quot;{model} gets {mpg} miles per gallon.&quot;</span>)</span>
525 <span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a><span class="co">#&gt; Mazda RX4 gets 21 miles per gallon.</span></span>
526 <span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a><span class="co">#&gt; Mazda RX4 Wag gets 21 miles per gallon.</span></span>
527 <span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="co">#&gt; Datsun 710 gets 22.8 miles per gallon.</span></span>
528 <span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co">#&gt; Hornet 4 Drive gets 21.4 miles per gallon.</span></span>
529 <span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a><span class="co">#&gt; Hornet Sportabout gets 18.7 miles per gallon.</span></span>
530 <span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a><span class="co">#&gt; Valiant gets 18.1 miles per gallon.</span></span></code></pre></div>
531 <p>Returning to <code>glue()</code>, recall that it defaults to
532 evaluation in the caller environment. This has happy implications inside
533 a <code>dplyr::mutate()</code> pipeline. The data-masking feature of
534 <code>mutate()</code> means the columns of the target data frame are “in
535 scope” for a <code>glue()</code> call:</p>
536 <div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
537 <span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a></span>
538 <span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a>mini_mtcars <span class="sc">|&gt;</span></span>
539 <span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">note =</span> <span class="fu">glue</span>(<span class="st">&quot;{model} gets {mpg} miles per gallon.&quot;</span>)) <span class="sc">|&gt;</span></span>
540 <span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a> <span class="fu">select</span>(note, cyl, disp)</span>
541 <span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a><span class="co">#&gt; note cyl disp</span></span>
542 <span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="co">#&gt; 1 Mazda RX4 gets 21 miles per gallon. 6 160</span></span>
543 <span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a><span class="co">#&gt; 2 Mazda RX4 Wag gets 21 miles per gallon. 6 160</span></span>
544 <span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a><span class="co">#&gt; 3 Datsun 710 gets 22.8 miles per gallon. 4 108</span></span>
545 <span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a><span class="co">#&gt; 4 Hornet 4 Drive gets 21.4 miles per gallon. 6 258</span></span>
546 <span id="cb18-11"><a href="#cb18-11" tabindex="-1"></a><span class="co">#&gt; 5 Hornet Sportabout gets 18.7 miles per gallon. 8 360</span></span>
547 <span id="cb18-12"><a href="#cb18-12" tabindex="-1"></a><span class="co">#&gt; 6 Valiant gets 18.1 miles per gallon. 6 225</span></span></code></pre></div>
548 </div>
549 <div id="sql" class="section level2">
550 <h2>SQL</h2>
551 <p>glue has explicit support for constructing SQL statements. Use
552 backticks to quote identifiers. Normal strings and numbers are quoted
553 appropriately for your backend.</p>
554 <div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>con <span class="ot">&lt;-</span> DBI<span class="sc">::</span><span class="fu">dbConnect</span>(RSQLite<span class="sc">::</span><span class="fu">SQLite</span>(), <span class="st">&quot;:memory:&quot;</span>)</span>
555 <span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="fu">colnames</span>(iris) <span class="ot">&lt;-</span> <span class="fu">gsub</span>(<span class="st">&quot;[.]&quot;</span>, <span class="st">&quot;_&quot;</span>, <span class="fu">tolower</span>(<span class="fu">colnames</span>(iris)))</span>
556 <span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a>DBI<span class="sc">::</span><span class="fu">dbWriteTable</span>(con, <span class="st">&quot;iris&quot;</span>, iris)</span>
557 <span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a>var <span class="ot">&lt;-</span> <span class="st">&quot;sepal_width&quot;</span></span>
558 <span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a>tbl <span class="ot">&lt;-</span> <span class="st">&quot;iris&quot;</span></span>
559 <span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a>num <span class="ot">&lt;-</span> <span class="dv">2</span></span>
560 <span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a>val <span class="ot">&lt;-</span> <span class="st">&quot;setosa&quot;</span></span>
561 <span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a><span class="fu">glue_sql</span>(<span class="st">&quot;</span></span>
562 <span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a><span class="st"> SELECT {`var`}</span></span>
563 <span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a><span class="st"> FROM {`tbl`}</span></span>
564 <span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a><span class="st"> WHERE {`tbl`}.sepal_length &gt; {num}</span></span>
565 <span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a><span class="st"> AND {`tbl`}.species = {val}</span></span>
566 <span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a><span class="st"> &quot;</span>, <span class="at">.con =</span> con)</span>
567 <span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a><span class="co">#&gt; &lt;SQL&gt; SELECT `sepal_width`</span></span>
568 <span id="cb19-15"><a href="#cb19-15" tabindex="-1"></a><span class="co">#&gt; FROM `iris`</span></span>
569 <span id="cb19-16"><a href="#cb19-16" tabindex="-1"></a><span class="co">#&gt; WHERE `iris`.sepal_length &gt; 2</span></span>
570 <span id="cb19-17"><a href="#cb19-17" tabindex="-1"></a><span class="co">#&gt; AND `iris`.species = &#39;setosa&#39;</span></span></code></pre></div>
571 <p><code>glue_sql()</code> can be used in conjunction with parameterized
572 queries using <code>DBI::dbBind()</code> to provide protection for SQL
573 Injection attacks.</p>
574 <div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>sql <span class="ot">&lt;-</span> <span class="fu">glue_sql</span>(<span class="st">&quot;</span></span>
575 <span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a><span class="st"> SELECT {`var`}</span></span>
576 <span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="st"> FROM {`tbl`}</span></span>
577 <span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a><span class="st"> WHERE {`tbl`}.sepal_length &gt; ?</span></span>
578 <span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a><span class="st">&quot;</span>, <span class="at">.con =</span> con)</span>
579 <span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a>query <span class="ot">&lt;-</span> DBI<span class="sc">::</span><span class="fu">dbSendQuery</span>(con, sql)</span>
580 <span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a>DBI<span class="sc">::</span><span class="fu">dbBind</span>(query, <span class="fu">list</span>(num))</span>
581 <span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a>DBI<span class="sc">::</span><span class="fu">dbFetch</span>(query, <span class="at">n =</span> <span class="dv">4</span>)</span>
582 <span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a><span class="co">#&gt; sepal_width</span></span>
583 <span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a><span class="co">#&gt; 1 3.5</span></span>
584 <span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a><span class="co">#&gt; 2 3.0</span></span>
585 <span id="cb20-12"><a href="#cb20-12" tabindex="-1"></a><span class="co">#&gt; 3 3.2</span></span>
586 <span id="cb20-13"><a href="#cb20-13" tabindex="-1"></a><span class="co">#&gt; 4 3.1</span></span>
587 <span id="cb20-14"><a href="#cb20-14" tabindex="-1"></a>DBI<span class="sc">::</span><span class="fu">dbClearResult</span>(query)</span></code></pre></div>
588 <p><code>glue_sql()</code> can be used to build up more complex queries
589 with interchangeable sub queries. It returns <code>DBI::SQL()</code>
590 objects which are properly protected from quoting.</p>
591 <div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>sub_query <span class="ot">&lt;-</span> <span class="fu">glue_sql</span>(<span class="st">&quot;</span></span>
592 <span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a><span class="st"> SELECT *</span></span>
593 <span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a><span class="st"> FROM {`tbl`}</span></span>
594 <span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a><span class="st"> &quot;</span>, <span class="at">.con =</span> con)</span>
595 <span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a></span>
596 <span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a><span class="fu">glue_sql</span>(<span class="st">&quot;</span></span>
597 <span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="st"> SELECT s.{`var`}</span></span>
598 <span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="st"> FROM ({sub_query}) AS s</span></span>
599 <span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="st"> &quot;</span>, <span class="at">.con =</span> con)</span>
600 <span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a><span class="co">#&gt; &lt;SQL&gt; SELECT s.`sepal_width`</span></span>
601 <span id="cb21-11"><a href="#cb21-11" tabindex="-1"></a><span class="co">#&gt; FROM (SELECT *</span></span>
602 <span id="cb21-12"><a href="#cb21-12" tabindex="-1"></a><span class="co">#&gt; FROM `iris`) AS s</span></span></code></pre></div>
603 <p>If you want to input multiple values for use in SQL IN statements put
604 <code>*</code> at the end of the value and the values will be collapsed
605 and quoted appropriately.</p>
606 <div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a><span class="fu">glue_sql</span>(<span class="st">&quot;SELECT * FROM {`tbl`} WHERE sepal_length IN ({vals*})&quot;</span>,</span>
607 <span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a> <span class="at">vals =</span> <span class="dv">1</span>, <span class="at">.con =</span> con)</span>
608 <span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a><span class="co">#&gt; &lt;SQL&gt; SELECT * FROM `iris` WHERE sepal_length IN (1)</span></span>
609 <span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a></span>
610 <span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="fu">glue_sql</span>(<span class="st">&quot;SELECT * FROM {`tbl`} WHERE sepal_length IN ({vals*})&quot;</span>,</span>
611 <span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a> <span class="at">vals =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">.con =</span> con)</span>
612 <span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a><span class="co">#&gt; &lt;SQL&gt; SELECT * FROM `iris` WHERE sepal_length IN (1, 2, 3, 4, 5)</span></span>
613 <span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a></span>
614 <span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a><span class="fu">glue_sql</span>(<span class="st">&quot;SELECT * FROM {`tbl`} WHERE species IN ({vals*})&quot;</span>,</span>
615 <span id="cb22-10"><a href="#cb22-10" tabindex="-1"></a> <span class="at">vals =</span> <span class="st">&quot;setosa&quot;</span>, <span class="at">.con =</span> con)</span>
616 <span id="cb22-11"><a href="#cb22-11" tabindex="-1"></a><span class="co">#&gt; &lt;SQL&gt; SELECT * FROM `iris` WHERE species IN (&#39;setosa&#39;)</span></span>
617 <span id="cb22-12"><a href="#cb22-12" tabindex="-1"></a></span>
618 <span id="cb22-13"><a href="#cb22-13" tabindex="-1"></a><span class="fu">glue_sql</span>(<span class="st">&quot;SELECT * FROM {`tbl`} WHERE species IN ({vals*})&quot;</span>,</span>
619 <span id="cb22-14"><a href="#cb22-14" tabindex="-1"></a> <span class="at">vals =</span> <span class="fu">c</span>(<span class="st">&quot;setosa&quot;</span>, <span class="st">&quot;versicolor&quot;</span>), <span class="at">.con =</span> con)</span>
620 <span id="cb22-15"><a href="#cb22-15" tabindex="-1"></a><span class="co">#&gt; &lt;SQL&gt; SELECT * FROM `iris` WHERE species IN (&#39;setosa&#39;, &#39;versicolor&#39;)</span></span></code></pre></div>
621 </div>
622
623
624
625 <!-- code folding -->
626
627
628 <!-- dynamically load mathjax for compatibility with self-contained -->
629 <script>
630 (function () {
631 var script = document.createElement("script");
632 script.type = "text/javascript";
633 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
634 document.getElementsByTagName("head")[0].appendChild(script);
635 })();
636 </script>
637
638 </body>
639 </html>