| 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>Transformers</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">Transformers</h1> |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
<p>Transformers allow you to apply functions to the glue input and |
| 345 |
output, before and after evaluation. This allows you to write things |
| 346 |
like <code>glue_sql()</code>, which automatically quotes variables for |
| 347 |
you or add a syntax for automatically collapsing outputs.</p> |
| 348 |
<p>The transformer functions simply take two arguments <code>text</code> |
| 349 |
and <code>envir</code>, where <code>text</code> is the unparsed string |
| 350 |
inside the glue block and <code>envir</code> is the execution |
| 351 |
environment. Most transformers will then call |
| 352 |
<code>eval(parse(text = text, keep.source = FALSE), envir)</code> which |
| 353 |
parses and evaluates the code.</p> |
| 354 |
<p>You can then supply the transformer function to glue with the |
| 355 |
<code>.transformer</code> argument. In this way users can manipulate the |
| 356 |
text before parsing and change the output after evaluation.</p> |
| 357 |
<p>It is often useful to write a <code>glue()</code> wrapper function |
| 358 |
which supplies a <code>.transformer</code> to <code>glue()</code> or |
| 359 |
<code>glue_data()</code> and potentially has additional arguments. One |
| 360 |
important consideration when doing this is to include |
| 361 |
<code>.envir = parent.frame()</code> in the wrapper to ensure the |
| 362 |
evaluation environment is correct.</p> |
| 363 |
<p>Some example implementations of potentially useful transformers |
| 364 |
follow. The aim right now is not to include most of these custom |
| 365 |
functions within the <code>glue</code> package. Rather, users are |
| 366 |
encouraged to create custom functions using transformers to fit their |
| 367 |
individual needs.</p> |
| 368 |
<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> |
| 369 |
<div id="collapse-transformer" class="section level3"> |
| 370 |
<h3>collapse transformer</h3> |
| 371 |
<p>A transformer which automatically collapses any glue block ending |
| 372 |
with <code>*</code>.</p> |
| 373 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>collapse_transformer <span class="ot"><-</span> <span class="cf">function</span>(<span class="at">regex =</span> <span class="st">"[*]$"</span>, ...) {</span> |
| 374 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a> <span class="cf">function</span>(text, envir) {</span> |
| 375 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a> collapse <span class="ot"><-</span> <span class="fu">grepl</span>(regex, text)</span> |
| 376 |
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a> <span class="cf">if</span> (collapse) {</span> |
| 377 |
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a> text <span class="ot"><-</span> <span class="fu">sub</span>(regex, <span class="st">""</span>, text)</span> |
| 378 |
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a> }</span> |
| 379 |
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a> res <span class="ot"><-</span> <span class="fu">identity_transformer</span>(text, envir)</span> |
| 380 |
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a> <span class="cf">if</span> (collapse) {</span> |
| 381 |
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a> <span class="fu">glue_collapse</span>(res, ...) </span> |
| 382 |
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a> } <span class="cf">else</span> {</span> |
| 383 |
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a> res</span> |
| 384 |
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a> }</span> |
| 385 |
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a> }</span> |
| 386 |
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a>}</span> |
| 387 |
<span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a></span> |
| 388 |
<span id="cb2-16"><a href="#cb2-16" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">"{1:5*}</span><span class="sc">\n</span><span class="st">{letters[1:5]*}"</span>, <span class="at">.transformer =</span> <span class="fu">collapse_transformer</span>(<span class="at">sep =</span> <span class="st">", "</span>))</span> |
| 389 |
<span id="cb2-17"><a href="#cb2-17" tabindex="-1"></a><span class="co">#> 1, 2, 3, 4, 5</span></span> |
| 390 |
<span id="cb2-18"><a href="#cb2-18" tabindex="-1"></a><span class="co">#> a, b, c, d, e</span></span> |
| 391 |
<span id="cb2-19"><a href="#cb2-19" tabindex="-1"></a></span> |
| 392 |
<span id="cb2-20"><a href="#cb2-20" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">"{1:5*}</span><span class="sc">\n</span><span class="st">{letters[1:5]*}"</span>, <span class="at">.transformer =</span> <span class="fu">collapse_transformer</span>(<span class="at">sep =</span> <span class="st">", "</span>, <span class="at">last =</span> <span class="st">" and "</span>))</span> |
| 393 |
<span id="cb2-21"><a href="#cb2-21" tabindex="-1"></a><span class="co">#> 1, 2, 3, 4 and 5</span></span> |
| 394 |
<span id="cb2-22"><a href="#cb2-22" tabindex="-1"></a><span class="co">#> a, b, c, d and e</span></span> |
| 395 |
<span id="cb2-23"><a href="#cb2-23" tabindex="-1"></a></span> |
| 396 |
<span id="cb2-24"><a href="#cb2-24" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"one"</span>, <span class="st">"two"</span>)</span> |
| 397 |
<span id="cb2-25"><a href="#cb2-25" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">"{x}: {1:5*}"</span>, <span class="at">.transformer =</span> <span class="fu">collapse_transformer</span>(<span class="at">sep =</span> <span class="st">", "</span>))</span> |
| 398 |
<span id="cb2-26"><a href="#cb2-26" tabindex="-1"></a><span class="co">#> one: 1, 2, 3, 4, 5</span></span> |
| 399 |
<span id="cb2-27"><a href="#cb2-27" tabindex="-1"></a><span class="co">#> two: 1, 2, 3, 4, 5</span></span></code></pre></div> |
| 400 |
</div> |
| 401 |
<div id="shell-quoting-transformer" class="section level3"> |
| 402 |
<h3>Shell quoting transformer</h3> |
| 403 |
<p>A transformer which automatically quotes variables for use in shell |
| 404 |
commands, e.g. via <code>system()</code> or <code>system2()</code>.</p> |
| 405 |
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>shell_transformer <span class="ot"><-</span> <span class="cf">function</span>(<span class="at">type =</span> <span class="fu">c</span>(<span class="st">"sh"</span>, <span class="st">"csh"</span>, <span class="st">"cmd"</span>, <span class="st">"cmd2"</span>)) {</span> |
| 406 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a> type <span class="ot"><-</span> <span class="fu">match.arg</span>(type)</span> |
| 407 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a> <span class="cf">function</span>(text, envir) {</span> |
| 408 |
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a> res <span class="ot"><-</span> <span class="fu">identity_transformer</span>(text, envir)</span> |
| 409 |
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a> <span class="fu">shQuote</span>(res)</span> |
| 410 |
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a> }</span> |
| 411 |
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a>}</span> |
| 412 |
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a></span> |
| 413 |
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a>glue_sh <span class="ot"><-</span> <span class="cf">function</span>(..., <span class="at">.envir =</span> <span class="fu">parent.frame</span>(), <span class="at">.type =</span> <span class="fu">c</span>(<span class="st">"sh"</span>, <span class="st">"csh"</span>, <span class="st">"cmd"</span>, <span class="st">"cmd2"</span>)) {</span> |
| 414 |
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a> .type <span class="ot"><-</span> <span class="fu">match.arg</span>(.type)</span> |
| 415 |
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a> <span class="fu">glue</span>(..., <span class="at">.envir =</span> .envir, <span class="at">.transformer =</span> <span class="fu">shell_transformer</span>(.type))</span> |
| 416 |
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a></span> |
| 417 |
<span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a>}</span> |
| 418 |
<span id="cb3-14"><a href="#cb3-14" tabindex="-1"></a></span> |
| 419 |
<span id="cb3-15"><a href="#cb3-15" tabindex="-1"></a>filename <span class="ot"><-</span> <span class="st">"test"</span></span> |
| 420 |
<span id="cb3-16"><a href="#cb3-16" tabindex="-1"></a><span class="fu">writeLines</span>(<span class="at">con =</span> filename, <span class="st">"hello!"</span>)</span> |
| 421 |
<span id="cb3-17"><a href="#cb3-17" tabindex="-1"></a></span> |
| 422 |
<span id="cb3-18"><a href="#cb3-18" tabindex="-1"></a>command <span class="ot"><-</span> <span class="fu">glue_sh</span>(<span class="st">"cat {filename}"</span>)</span> |
| 423 |
<span id="cb3-19"><a href="#cb3-19" tabindex="-1"></a>command</span> |
| 424 |
<span id="cb3-20"><a href="#cb3-20" tabindex="-1"></a><span class="co">#> cat 'test'</span></span> |
| 425 |
<span id="cb3-21"><a href="#cb3-21" tabindex="-1"></a><span class="fu">system</span>(command)</span></code></pre></div> |
| 426 |
</div> |
| 427 |
<div id="emoji-transformer" class="section level3"> |
| 428 |
<h3>emoji transformer</h3> |
| 429 |
<p>A transformer which converts the text to the equivalent emoji.</p> |
| 430 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>emoji_transformer <span class="ot"><-</span> <span class="cf">function</span>(text, envir) {</span> |
| 431 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">grepl</span>(<span class="st">"[*]$"</span>, text)) {</span> |
| 432 |
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a> text <span class="ot"><-</span> <span class="fu">sub</span>(<span class="st">"[*]$"</span>, <span class="st">""</span>, text)</span> |
| 433 |
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a> <span class="fu">glue_collapse</span>(<span class="fu">ji_find</span>(text)<span class="sc">$</span>emoji)</span> |
| 434 |
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a> } <span class="cf">else</span> {</span> |
| 435 |
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a> <span class="fu">ji</span>(text)</span> |
| 436 |
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a> }</span> |
| 437 |
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a>}</span> |
| 438 |
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a></span> |
| 439 |
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a>glue_ji <span class="ot"><-</span> <span class="cf">function</span>(..., <span class="at">.envir =</span> <span class="fu">parent.frame</span>()) {</span> |
| 440 |
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a> <span class="fu">glue</span>(..., <span class="at">.open =</span> <span class="st">":"</span>, <span class="at">.close =</span> <span class="st">":"</span>, <span class="at">.envir =</span> .envir, <span class="at">.transformer =</span> emoji_transformer)</span> |
| 441 |
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a>}</span> |
| 442 |
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="fu">glue_ji</span>(<span class="st">"one :heart:"</span>)</span> |
| 443 |
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="fu">glue_ji</span>(<span class="st">"many :heart*:"</span>)</span></code></pre></div> |
| 444 |
</div> |
| 445 |
<div id="sprintf-transformer" class="section level3"> |
| 446 |
<h3>sprintf transformer</h3> |
| 447 |
<p>A transformer which allows succinct <code>sprintf</code> format |
| 448 |
strings.</p> |
| 449 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>sprintf_transformer <span class="ot"><-</span> <span class="cf">function</span>(text, envir) {</span> |
| 450 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a> m <span class="ot"><-</span> <span class="fu">regexpr</span>(<span class="st">":.+$"</span>, text)</span> |
| 451 |
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a> <span class="cf">if</span> (m <span class="sc">!=</span> <span class="sc">-</span><span class="dv">1</span>) {</span> |
| 452 |
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a> format <span class="ot"><-</span> <span class="fu">substring</span>(<span class="fu">regmatches</span>(text, m), <span class="dv">2</span>)</span> |
| 453 |
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a> <span class="fu">regmatches</span>(text, m) <span class="ot"><-</span> <span class="st">""</span></span> |
| 454 |
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a> res <span class="ot"><-</span> <span class="fu">identity_transformer</span>(text, envir)</span> |
| 455 |
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a> <span class="fu">do.call</span>(sprintf, <span class="fu">list</span>(<span class="fu">glue</span>(<span class="st">"%{format}"</span>), res))</span> |
| 456 |
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a> } <span class="cf">else</span> {</span> |
| 457 |
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a> <span class="fu">identity_transformer</span>(text, envir)</span> |
| 458 |
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a> }</span> |
| 459 |
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a>}</span> |
| 460 |
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a></span> |
| 461 |
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a>glue_fmt <span class="ot"><-</span> <span class="cf">function</span>(..., <span class="at">.envir =</span> <span class="fu">parent.frame</span>()) {</span> |
| 462 |
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a> <span class="fu">glue</span>(..., <span class="at">.transformer =</span> sprintf_transformer, <span class="at">.envir =</span> .envir)</span> |
| 463 |
<span id="cb5-15"><a href="#cb5-15" tabindex="-1"></a>}</span> |
| 464 |
<span id="cb5-16"><a href="#cb5-16" tabindex="-1"></a><span class="fu">glue_fmt</span>(<span class="st">"π = {pi:.3f}"</span>)</span> |
| 465 |
<span id="cb5-17"><a href="#cb5-17" tabindex="-1"></a><span class="co">#> π = 3.142</span></span></code></pre></div> |
| 466 |
</div> |
| 467 |
<div id="signif-transformer" class="section level3"> |
| 468 |
<h3>signif transformer</h3> |
| 469 |
<p>A transformer generator that represents numbers with a given number |
| 470 |
of significant digits. This is useful if we want to represent all |
| 471 |
numbers using the same significant digits</p> |
| 472 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>signif_transformer <span class="ot"><-</span> <span class="cf">function</span>(<span class="at">digits =</span> <span class="dv">3</span>) {</span> |
| 473 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a> <span class="fu">force</span>(digits)</span> |
| 474 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a> <span class="cf">function</span>(text, envir) {</span> |
| 475 |
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a> x <span class="ot"><-</span> <span class="fu">identity_transformer</span>(text, envir)</span> |
| 476 |
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">is.numeric</span>(x)) {</span> |
| 477 |
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a> <span class="fu">signif</span>(x, <span class="at">digits =</span> digits)</span> |
| 478 |
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a> } <span class="cf">else</span> {</span> |
| 479 |
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a> x</span> |
| 480 |
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a> }</span> |
| 481 |
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a> }</span> |
| 482 |
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a>}</span> |
| 483 |
<span id="cb6-12"><a href="#cb6-12" tabindex="-1"></a>glue_signif <span class="ot"><-</span> <span class="cf">function</span>(..., <span class="at">.envir =</span> <span class="fu">parent.frame</span>()) {</span> |
| 484 |
<span id="cb6-13"><a href="#cb6-13" tabindex="-1"></a> <span class="fu">glue</span>(..., <span class="at">.transformer =</span> <span class="fu">signif_transformer</span>(<span class="dv">3</span>), <span class="at">.envir =</span> .envir)</span> |
| 485 |
<span id="cb6-14"><a href="#cb6-14" tabindex="-1"></a>}</span> |
| 486 |
<span id="cb6-15"><a href="#cb6-15" tabindex="-1"></a></span> |
| 487 |
<span id="cb6-16"><a href="#cb6-16" tabindex="-1"></a><span class="fu">glue_signif</span>(<span class="st">"π = {pi}; 10π = {10*pi}; 100π = {100*pi}"</span>)</span> |
| 488 |
<span id="cb6-17"><a href="#cb6-17" tabindex="-1"></a><span class="co">#> π = 3.14; 10π = 31.4; 100π = 314</span></span></code></pre></div> |
| 489 |
</div> |
| 490 |
<div id="safely-transformer" class="section level3"> |
| 491 |
<h3>safely transformer</h3> |
| 492 |
<p>A transformer that acts like <code>purrr::safely()</code>, which |
| 493 |
returns a value instead of an error.</p> |
| 494 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>safely_transformer <span class="ot"><-</span> <span class="cf">function</span>(<span class="at">otherwise =</span> <span class="cn">NA</span>) {</span> |
| 495 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a> <span class="cf">function</span>(text, envir) {</span> |
| 496 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a> <span class="fu">tryCatch</span>(</span> |
| 497 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a> <span class="fu">identity_transformer</span>(text, envir),</span> |
| 498 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a> <span class="at">error =</span> <span class="cf">function</span>(e) <span class="cf">if</span> (<span class="fu">is.language</span>(otherwise)) <span class="fu">eval</span>(otherwise) <span class="cf">else</span> otherwise)</span> |
| 499 |
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a> }</span> |
| 500 |
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a>}</span> |
| 501 |
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a></span> |
| 502 |
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a>glue_safely <span class="ot"><-</span> <span class="cf">function</span>(..., <span class="at">.otherwise =</span> <span class="cn">NA</span>, <span class="at">.envir =</span> <span class="fu">parent.frame</span>()) {</span> |
| 503 |
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a> <span class="fu">glue</span>(..., <span class="at">.transformer =</span> <span class="fu">safely_transformer</span>(.otherwise), <span class="at">.envir =</span> .envir)</span> |
| 504 |
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a>}</span> |
| 505 |
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a></span> |
| 506 |
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a><span class="co"># Default returns missing if there is an error</span></span> |
| 507 |
<span id="cb7-14"><a href="#cb7-14" tabindex="-1"></a><span class="fu">glue_safely</span>(<span class="st">"foo: {xyz}"</span>)</span> |
| 508 |
<span id="cb7-15"><a href="#cb7-15" tabindex="-1"></a><span class="co">#> foo: NA</span></span> |
| 509 |
<span id="cb7-16"><a href="#cb7-16" tabindex="-1"></a></span> |
| 510 |
<span id="cb7-17"><a href="#cb7-17" tabindex="-1"></a><span class="co"># Or an empty string</span></span> |
| 511 |
<span id="cb7-18"><a href="#cb7-18" tabindex="-1"></a><span class="fu">glue_safely</span>(<span class="st">"foo: {xyz}"</span>, <span class="at">.otherwise =</span> <span class="st">"Error"</span>)</span> |
| 512 |
<span id="cb7-19"><a href="#cb7-19" tabindex="-1"></a><span class="co">#> foo: Error</span></span> |
| 513 |
<span id="cb7-20"><a href="#cb7-20" tabindex="-1"></a></span> |
| 514 |
<span id="cb7-21"><a href="#cb7-21" tabindex="-1"></a><span class="co"># Or output the error message in red</span></span> |
| 515 |
<span id="cb7-22"><a href="#cb7-22" tabindex="-1"></a><span class="fu">library</span>(crayon)</span> |
| 516 |
<span id="cb7-23"><a href="#cb7-23" tabindex="-1"></a><span class="fu">glue_safely</span>(<span class="st">"foo: {xyz}"</span>, <span class="at">.otherwise =</span> <span class="fu">quote</span>(<span class="fu">glue</span>(<span class="st">"{red}Error: {conditionMessage(e)}{reset}"</span>)))</span> |
| 517 |
<span id="cb7-24"><a href="#cb7-24" tabindex="-1"></a><span class="co">#> foo: Error: Failed to evaluate glue component {xyz}</span></span> |
| 518 |
<span id="cb7-25"><a href="#cb7-25" tabindex="-1"></a><span class="co">#> Caused by error:</span></span> |
| 519 |
<span id="cb7-26"><a href="#cb7-26" tabindex="-1"></a><span class="co">#> ! object 'xyz' not found</span></span></code></pre></div> |
| 520 |
</div> |
| 521 |
<div id="variables-and-values-transformer" class="section level3"> |
| 522 |
<h3>“Variables and Values” transformer</h3> |
| 523 |
<p>A transformer that expands input of the form <code>{var_name=}</code> |
| 524 |
into <code>var_name = var_value</code>, i.e. a shorthand for exposing |
| 525 |
variable names with their values. This is inspired by an <a href="https://docs.python.org/3.8/whatsnew/3.8.html#f-strings-now-support-for-quick-and-easy-debugging">f-strings |
| 526 |
feature coming in Python 3.8</a>. It’s actually more general: you can |
| 527 |
use it with an expression input such as <code>{expr=}</code>.</p> |
| 528 |
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>vv_transformer <span class="ot"><-</span> <span class="cf">function</span>(text, envir) {</span> |
| 529 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> regex <span class="ot"><-</span> <span class="st">"=$"</span></span> |
| 530 |
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">grepl</span>(regex, text)) {</span> |
| 531 |
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">identity_transformer</span>(text, envir))</span> |
| 532 |
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a> }</span> |
| 533 |
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a></span> |
| 534 |
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a> text <span class="ot"><-</span> <span class="fu">sub</span>(regex, <span class="st">""</span>, text)</span> |
| 535 |
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a> res <span class="ot"><-</span> <span class="fu">identity_transformer</span>(text, envir)</span> |
| 536 |
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a> n <span class="ot"><-</span> <span class="fu">length</span>(res)</span> |
| 537 |
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a> res <span class="ot"><-</span> <span class="fu">glue_collapse</span>(res, <span class="at">sep =</span> <span class="st">", "</span>)</span> |
| 538 |
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a> <span class="cf">if</span> (n <span class="sc">></span> <span class="dv">1</span>) {</span> |
| 539 |
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a> res <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"["</span>, res, <span class="st">"]"</span>)</span> |
| 540 |
<span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a> }</span> |
| 541 |
<span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a> <span class="fu">glue_collapse</span>(<span class="fu">c</span>(text, <span class="st">" = "</span>, res))</span> |
| 542 |
<span id="cb8-15"><a href="#cb8-15" tabindex="-1"></a>}</span></code></pre></div> |
| 543 |
<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="fu">set.seed</span>(<span class="dv">1234</span>)</span> |
| 544 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>description <span class="ot"><-</span> <span class="st">"some random"</span></span> |
| 545 |
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a>numbers <span class="ot"><-</span> <span class="fu">sample</span>(<span class="dv">100</span>, <span class="dv">4</span>)</span> |
| 546 |
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a>average <span class="ot"><-</span> <span class="fu">mean</span>(numbers)</span> |
| 547 |
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a>sum <span class="ot"><-</span> <span class="fu">sum</span>(numbers)</span> |
| 548 |
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a></span> |
| 549 |
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">"For {description} {numbers=}, {average=}, {sum=}."</span>, <span class="at">.transformer =</span> vv_transformer)</span> |
| 550 |
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#> For some random numbers = [28, 80, 22, 9], average = 34.75, sum = 139.</span></span> |
| 551 |
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a></span> |
| 552 |
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a>a <span class="ot"><-</span> <span class="dv">3</span></span> |
| 553 |
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a>b <span class="ot"><-</span> <span class="fl">5.6</span></span> |
| 554 |
<span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a><span class="fu">glue</span>(<span class="st">"{a=}</span><span class="sc">\n</span><span class="st">{b=}</span><span class="sc">\n</span><span class="st">{a * 9 + b * 2=}"</span>, <span class="at">.transformer =</span> vv_transformer)</span> |
| 555 |
<span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a><span class="co">#> a = 3</span></span> |
| 556 |
<span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a><span class="co">#> b = 5.6</span></span> |
| 557 |
<span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a><span class="co">#> a * 9 + b * 2 = 38.2</span></span></code></pre></div> |
| 558 |
</div> |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
<!-- code folding --> |
| 563 |
|
| 564 |
|
| 565 |
<!-- dynamically load mathjax for compatibility with self-contained --> |
| 566 |
<script> |
| 567 |
(function () { |
| 568 |
var script = document.createElement("script"); |
| 569 |
script.type = "text/javascript"; |
| 570 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 571 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 572 |
})(); |
| 573 |
</script> |
| 574 |
|
| 575 |
</body> |
| 576 |
</html> |