Letter loops with knitr

I am wondering if there is an easy way to create a bunch of tables or graphics with variable headers in knitr . The only way I know is: (simplified from https://github.com/yihui/knitr-examples/blob/master/075-knit-expand.Rnw ). But you need to drag the output to src and then print it after the loop, because I want to write a function to create such a loop from an arbitrary data set.

 \documentclass{article} \title{Using knit\_expand() for templates} \author{Yihui Xie} \begin{document} \maketitle \tableofcontents <<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))} \end{document} 

So what I want to do is something like this:

 \documentclass{article} \title{Using knit\_expand() for templates} \author{Yihui Xie} \begin{document} \maketitle \tableofcontents <<lm, tidy.opts=list(width.cutoff=55)>>= myLfFun=function(dataset){ ... some function definition which produces say an lm for each variable in dataset ... } @ \Sexpr{myLfFun(Titanic} ... \Sexpr{myLfFun(mtcars} ... etc \end{document} 

... What if I run brew (), this will create ...

 \documentclass{article} \title{Brew + knitR} \author{Ramnath Vaidyanathan} \begin{document} \maketitle \tableofcontents <<lm-cyl >>= lm(mpg ~ cyl, data = mtcars) @ <<lm-disp >>= lm(mpg ~ disp, data = mtcars) @ <<lm-hp >>= lm(mpg ~ hp, data = mtcars) @ <<lm-drat >>= lm(mpg ~ drat, data = mtcars) @ <<lm-wt >>= lm(mpg ~ wt, data = mtcars) @ <<lm-qsec >>= lm(mpg ~ qsec, data = mtcars) @ <<lm-vs >>= lm(mpg ~ vs, data = mtcars) @ <<lm-am >>= lm(mpg ~ am, data = mtcars) @ <<lm-gear >>= lm(mpg ~ gear, data = mtcars) @ <<lm-carb >>= lm(mpg ~ carb, data = mtcars) @ ((... same for Titanic database ...)) \end{document} 

... and the output of this I could then knit2pdf (). Therefore, if the template was called tmpl.Rnw, I would do brew ('tmpl.Rnw', 'doc.Rnw'); knit2pdf ('doc.Rnw)

+5
source share
3 answers

I don’t understand why you need knit_expand when the good old sprintf can do the same. Here is the result: http://www.anst.uu.se/chrba104/stackoverflow/output.pdf .

Although my template is also configured for the mtcars , I don’t see how you could make it simpler without losing flexibility.

 \documentclass{article} \title{Not using knit\_expand() for templates} \author{Yihui Xie} \begin{document} \maketitle \tableofcontents <<lm-mtcars, tidy.opts=list(width.cutoff=55)>>= vars <- setdiff(names(mtcars), 'mpg') src <- sprintf( paste('\\subsection{Regression on %s}', '<<lm-%s>>=', 'lm(mpg ~ %s, data=mtcars)', '@', sep='\n'), vars, vars, vars) @ \Sexpr{knit(text = src)} \end{document} 
+3
source

I prefer to use specialized template libraries such as whisker and brew to achieve what you are looking for, since trying to write latex code using the R IMHO function is pretty ugly. The template file is shown below and is called tpl.Rnw . You can convert it to pdf by executing the following commands. You can easily write a function to encapsulate this logic, which converts brew patterns to pdf using knitr.

 brew('tpl.Rnw', 'doc.Rnw') knit2pdf('doc.Rnw') 

tpl.Rnw template tpl.Rnw

 \documentclass{article} \title{Brew + knitR} \author{Ramnath Vaidyanathan} \begin{document} \maketitle \tableofcontents <% for (xvar in names(mtcars)[-1]) { %> \subsection{Regression on <%= xvar %>} <<lm-<%= xvar %> >>= lm(mpg ~ <%= xvar %>, data = mtcars) @ <% } %> \end{document} 
+3
source

I found out why I could not put the string \Sexpr{knit(text = unlist(src))} inside the previous normal code snippet. I needed to set opts_knit$set(progress = F, verbose = F) at the beginning of the document and set at least the comment=NA, warning=FALSE,message=FALSE,echo=FALSE for the fragment. This simple move allows you to insert a lot of lines like knit(text = unlist(src)) wherever I want, and as many times as I want in a piece. This eliminates the need for a dedicated function.

0
source

Source: https://habr.com/ru/post/974444/


All Articles