-
Notifications
You must be signed in to change notification settings - Fork 586
/
Copy path075-knit-expand.Rnw
43 lines (33 loc) · 1.04 KB
/
075-knit-expand.Rnw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
\documentclass{article}
\title{Using knit\_expand() for templates}
\author{Yihui Xie}
\begin{document}
\maketitle
\tableofcontents
\section{Write one row of data}
Only the first two sections are evaluated.
<<run-all, include=FALSE>>=
library(knitr)
src = NULL
for (i in 1:5) src = c(src, knit_expand('075-template.rnw'))
@
\Sexpr{paste(knit(text = src), collapse = '\n')}
\section{A regression model on several variables}
You can expand a template file, or just provide the template as a character string.
<<lm-mtcars, tidy.opts=list(width.cutoff=55)>>=
# the template
tpl = c("\\subsection{Regression on {{xvar}}}",
"<<lm-{{xvar}}>>=",
"lm(mpg~{{xvar}}, data=mtcars)",
"@")
# expand to knitr source and pass to knit()
src = lapply(names(mtcars)[-1], function(xvar) {knit_expand(text = tpl)})
@
\Sexpr{knit(text = unlist(src))}
\section{Multiple variables}
<<multiple-tpl>>=
tpl = 'The value of a is {{a}} and b is {{b}}.'
@
\Sexpr{knit_expand(text = tpl, a = 1, b = 2)}
\Sexpr{knit_expand(text = tpl, a = 5, b = 2013)}
\end{document}