158 lines
4.9 KiB
HTML
158 lines
4.9 KiB
HTML
|
<!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>#> [1] "The value of pi is 3.14159265358979."
|
||
|
</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>#> [1] "The value of a is 0.676489790550228, so a + 1 is 1.67648979055023."
|
||
|
</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>#> [1] "The area of a circle with radius 5 is 78.5398163397448"
|
||
|
</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>#> [1] "a is 1 and b is 2, with my own pi being 3 instead of 3.14159265358979"
|
||
|
</code></pre>
|
||
|
<p>Custom delimiter <code><% %></code>:</p>
|
||
|
<pre><code class="language-r">knit_expand(text = 'I do not like curly braces, so use % with <> instead: a is <% a %>.', a = 8, delim = c("<%", "%>"))
|
||
|
</code></pre>
|
||
|
<pre><code>#> [1] "I do not like curly braces, so use % with <> instead: a is 8."
|
||
|
</code></pre>
|
||
|
<p>The pyexpander delimiter:</p>
|
||
|
<pre><code class="language-r">knit_expand(text = 'hello $(LETTERS[24]) and $(pi)!', delim = c("$(", ")"))
|
||
|
</code></pre>
|
||
|
<pre><code>#> [1] "hello X and 3.14159265358979!"
|
||
|
</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>#> [1] "you cannot see the value of x but it is indeed created: x = -0.320718473271458"
|
||
|
</code></pre>
|
||
|
<pre><code class="language-r">res = knit_expand(text = c(' x | x^2', '{{x=1:5;paste(sprintf("%2d | %3d", x, x^2), collapse = "\n")}}'))
|
||
|
cat(res)
|
||
|
</code></pre>
|
||
|
<pre><code>#> x | x^2
|
||
|
#> 1 | 1
|
||
|
#> 2 | 4
|
||
|
#> 3 | 9
|
||
|
#> 4 | 16
|
||
|
#> 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<<-i+1;sprintf("<h2>%d. %s</h2>", i, x)} }}<html>',
|
||
|
'{{h2("First Section")}}', '{{h2("Second Section")}}', '{{h2("Conclusion")}}', '</html>'))
|
||
|
cat(res)
|
||
|
</code></pre>
|
||
|
<pre><code>#> <html>
|
||
|
#> <h2>1. First Section</h2>
|
||
|
#> <h2>2. Second Section</h2>
|
||
|
#> <h2>3. Conclusion</h2>
|
||
|
#> </html>
|
||
|
</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("# Regression on {{i}}", '```{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>
|