158 lines
4.9 KiB
HTML
Raw Permalink Normal View History

2025-01-12 00:52:51 +08:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="generator" content="litedown 0.4">
<title>Templating with knit_expand()</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xiee/utils@1.13.44/css/prism-xcode.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xiee/utils@1.13.44/css/default.min.css">
</head>
<body>
<div class="frontmatter">
<div class="title"><h1>Templating with knit_expand()</h1></div>
<div class="author"><h2>Yihui Xie</h2></div>
<div class="date"><h3>2024-11-06</h3></div>
</div>
<div class="body">
<!--
%\VignetteEngine{litedown::vignette}
%\VignetteIndexEntry{Templating with knit_expand()}
-->
<p>A few simple examples:</p>
<pre><code class="language-r">library(knitr)
knit_expand(text = 'The value of pi is {{pi}}.')
</code></pre>
<pre><code>#&gt; [1] &quot;The value of pi is 3.14159265358979.&quot;
</code></pre>
<pre><code class="language-r">knit_expand(text = 'The value of a is {{a}}, so a + 1 is {{a+1}}.', a = rnorm(1))
</code></pre>
<pre><code>#&gt; [1] &quot;The value of a is 0.676489790550228, so a + 1 is 1.67648979055023.&quot;
</code></pre>
<pre><code class="language-r">knit_expand(text = 'The area of a circle with radius {{r}} is {{pi*r^2}}', r = 5)
</code></pre>
<pre><code>#&gt; [1] &quot;The area of a circle with radius 5 is 78.5398163397448&quot;
</code></pre>
<p>Any number of variables:</p>
<pre><code class="language-r">knit_expand(text = 'a is {{a}} and b is {{b}}, with my own pi being {{pi}} instead of {{base::pi}}', a=1, b=2, pi=3)
</code></pre>
<pre><code>#&gt; [1] &quot;a is 1 and b is 2, with my own pi being 3 instead of 3.14159265358979&quot;
</code></pre>
<p>Custom delimiter <code>&lt;% %&gt;</code>:</p>
<pre><code class="language-r">knit_expand(text = 'I do not like curly braces, so use % with &lt;&gt; instead: a is &lt;% a %&gt;.', a = 8, delim = c(&quot;&lt;%&quot;, &quot;%&gt;&quot;))
</code></pre>
<pre><code>#&gt; [1] &quot;I do not like curly braces, so use % with &lt;&gt; instead: a is 8.&quot;
</code></pre>
<p>The pyexpander delimiter:</p>
<pre><code class="language-r">knit_expand(text = 'hello $(LETTERS[24]) and $(pi)!', delim = c(&quot;$(&quot;, &quot;)&quot;))
</code></pre>
<pre><code>#&gt; [1] &quot;hello X and 3.14159265358979!&quot;
</code></pre>
<p>Arbitrary R code:</p>
<pre><code class="language-r">knit_expand(text = 'you cannot see the value of x {{x=rnorm(1)}}but it is indeed created: x = {{x}}')
</code></pre>
<pre><code>#&gt; [1] &quot;you cannot see the value of x but it is indeed created: x = -0.320718473271458&quot;
</code></pre>
<pre><code class="language-r">res = knit_expand(text = c(' x | x^2', '{{x=1:5;paste(sprintf(&quot;%2d | %3d&quot;, x, x^2), collapse = &quot;\n&quot;)}}'))
cat(res)
</code></pre>
<pre><code>#&gt; x | x^2
#&gt; 1 | 1
#&gt; 2 | 4
#&gt; 3 | 9
#&gt; 4 | 16
#&gt; 5 | 25
</code></pre>
<p>The m4 example: <a href="https://en.wikipedia.org/wiki/M4_(computer_language)">https://en.wikipedia.org/wiki/M4_(computer_language)</a></p>
<pre><code class="language-r">res = knit_expand(text = c('{{i=0;h2=function(x){i&lt;&lt;-i+1;sprintf(&quot;&lt;h2&gt;%d. %s&lt;/h2&gt;&quot;, i, x)} }}&lt;html&gt;',
'{{h2(&quot;First Section&quot;)}}', '{{h2(&quot;Second Section&quot;)}}', '{{h2(&quot;Conclusion&quot;)}}', '&lt;/html&gt;'))
cat(res)
</code></pre>
<pre><code>#&gt; &lt;html&gt;
#&gt; &lt;h2&gt;1. First Section&lt;/h2&gt;
#&gt; &lt;h2&gt;2. Second Section&lt;/h2&gt;
#&gt; &lt;h2&gt;3. Conclusion&lt;/h2&gt;
#&gt; &lt;/html&gt;
</code></pre>
<p>Build regression models based on a template; loop through some variables in <code>mtcars</code>:</p>
<pre><code class="language-r">src = lapply(names(mtcars)[2:5], function(i) {
knit_expand(text=c(&quot;# Regression on {{i}}&quot;, '```{r lm-{{i}}}', 'lm(mpg~{{i}}, data=mtcars)', '```', ''))
})
# knit the source
litedown::fuse(unlist(src), 'markdown')
</code></pre>
<pre><code># Regression on cyl
``` {.r}
lm(mpg~cyl, data=mtcars)
```
```
Call:
lm(formula = mpg ~ cyl, data = mtcars)
Coefficients:
(Intercept) cyl
37.885 -2.876
```
# Regression on disp
``` {.r}
lm(mpg~disp, data=mtcars)
```
```
Call:
lm(formula = mpg ~ disp, data = mtcars)
Coefficients:
(Intercept) disp
29.59985 -0.04122
```
# Regression on hp
``` {.r}
lm(mpg~hp, data=mtcars)
```
```
Call:
lm(formula = mpg ~ hp, data = mtcars)
Coefficients:
(Intercept) hp
30.09886 -0.06823
```
# Regression on drat
``` {.r}
lm(mpg~drat, data=mtcars)
```
```
Call:
lm(formula = mpg ~ drat, data = mtcars)
Coefficients:
(Intercept) drat
-7.525 7.678
```
</code></pre>
</div>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js" defer></script>
</body>
</html>