简体   繁体   English

带编织的字幕循环

[英]Loops with captions with knitr

I am wondering if there is an easy way to produce a bunch of tables or graphics with variable captions in knitr . 我想知道是否有一种简单的方法可以在knitr生成一堆带有可变标题的表格或图形。 The only way I know is this: (simplified from https://github.com/yihui/knitr-examples/blob/master/075-knit-expand.Rnw ). 我知道的唯一方法是:(简化自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. 但是将输出收集到src然后在循环之后打印它是一种拖累,因为我想编写一个函数来从任意数据集生成这样的循环。

\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 ... ...如果我在它上面运行brew()会产生......

\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(). ...然后我可以输出knit2pdf()。 So if the template were called tmpl.Rnw, I would run brew('tmpl.Rnw','doc.Rnw');knit2pdf('doc.Rnw) 因此,如果模板名为tmpl.Rnw,我将运行brew('tmpl.Rnw','doc.Rnw'); knit2pdf('doc.Rnw)

I don't see why you need knit_expand when good old sprintf can do the same. 我不明白为什么你需要knit_expand好老的sprintf可以做同样的事情。 Here is the output: http://www.anst.uu.se/chrba104/stackoverflow/output.pdf . 以下是输出: 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. 虽然我的模板也是为mtcars数据集定制的,但我没有看到如何在不失去灵活性的情况下简化它。

\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}

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. 我更喜欢使用像whiskerbrew这样的专用模板库来实现你所寻求的东西,因为尝试用R函数编写乳胶代码恕我直言很难看。 The template file is shown below and named tpl.Rnw . 模板文件如下所示,名为tpl.Rnw You can turn it into a pdf by running the following commands. 您可以通过运行以下命令将其转换为pdf。 You can easily writeup a function to encapsulates this logic that transforms brew templates into pdf using knitr. 您可以轻松编写一个函数来封装这个逻辑,使用knitr将brew模板转换为pdf。

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

Template File tpl.Rnw 模板文件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}

I found out why I couldn't put the \\Sexpr{knit(text = unlist(src))} line inside the previous normal code chunk. 我发现为什么我不能将\\Sexpr{knit(text = unlist(src))}行放在上一个普通代码块中。 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. 我需要在doc的开头设置opts_knit$set(progress = F, verbose = F) ,并为chunk设置至少一些comment=NA, warning=FALSE,message=FALSE,echo=FALSE 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. 这个简单的移动可以在任何我想要的地方粘贴像knit(text = unlist(src))这样的行,并且可以在一个块中粘贴多少次。 This obviates the need for a dedicated function. 这消除了对专用功能的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM