| 1 |
library(knitr) |
| 2 |
knit_expand(text = 'The value of pi is {{pi}}.') |
| 3 |
knit_expand(text = 'The value of a is {{a}}, so a + 1 is {{a+1}}.', a = rnorm(1)) |
| 4 |
knit_expand(text = 'The area of a circle with radius {{r}} is {{pi*r^2}}', r = 5) |
| 5 |
|
| 6 |
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) |
| 7 |
|
| 8 |
knit_expand(text = 'I do not like curly braces, so use % with <> instead: a is <% a %>.', a = 8, delim = c("<%", "%>")) |
| 9 |
|
| 10 |
knit_expand(text = 'hello $(LETTERS[24]) and $(pi)!', delim = c("$(", ")")) |
| 11 |
|
| 12 |
knit_expand(text = 'you cannot see the value of x {{x=rnorm(1)}}but it is indeed created: x = {{x}}') |
| 13 |
res = knit_expand(text = c(' x | x^2', '{{x=1:5;paste(sprintf("%2d | %3d", x, x^2), collapse = "\n")}}')) |
| 14 |
cat(res) |
| 15 |
|
| 16 |
res = knit_expand(text = c('{{i=0;h2=function(x){i<<-i+1;sprintf("<h2>%d. %s</h2>", i, x)} }}<html>', |
| 17 |
'{{h2("First Section")}}', '{{h2("Second Section")}}', '{{h2("Conclusion")}}', '</html>')) |
| 18 |
cat(res) |
| 19 |
|
| 20 |
src = lapply(names(mtcars)[2:5], function(i) { |
| 21 |
knit_expand(text=c("# Regression on {{i}}", '```{r lm-{{i}}}', 'lm(mpg~{{i}}, data=mtcars)', '```', '')) |
| 22 |
}) |
| 23 |
# knit the source |
| 24 |
litedown::fuse(unlist(src), 'markdown') |
| 25 |
|