Loops with captions with knitr

帅比萌擦擦* 提交于 2019-11-30 14:19:06

问题


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

\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 be able to do instead 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}

... Which if I ran brew() on it would produce ...

\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 the this I could then knit2pdf(). So if the template were called tmpl.Rnw, I would run brew('tmpl.Rnw','doc.Rnw');knit2pdf('doc.Rnw)


回答1:


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

Although my template is also custom made for the mtcars dataset, 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}



回答2:


I prefer to use dedicated templating libraries like whisker and brew to achieve what you are seeking, since trying to write latex code using an R function IMHO is plain ugly. The template file is shown below and named tpl.Rnw. You can turn it into a pdf by running the following commands. You can easily writeup a function to encapsulates this logic that transforms brew templates into pdf using knitr.

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

Template File 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:


I found out why I couldn't put the \Sexpr{knit(text = unlist(src))} line inside the previous normal code chunk. I needed to set opts_knit$set(progress = F, verbose = F) at the start of the doc and set at least some of comment=NA, warning=FALSE,message=FALSE,echo=FALSE for the chunk. This simple move makes it much to paste lines like knit(text = unlist(src)) wherever I want and as many times I want in a chunk. This obviates the need for a dedicated function.



来源:https://stackoverflow.com/questions/14316529/loops-with-captions-with-knitr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!