本文介绍了带编织物字幕的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法可以在knitr中生成带有可变标题的一堆表或图形.我知道的唯一方法是:(从 https:简化为: //github.com/yihui/knitr-examples/blob/master/075-knit-expand.Rnw ).但是,将输出收集到src中然后在循环之后打印它是一件很麻烦的事情,因为我想编写一个函数来从任意数据集中产生这样的循环.

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}

...如果我在上面运行brew()会产生...

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

...,然后我可以输出knit2pdf()的输出.因此,如果该模板名为tmpl.Rnw,我将运行brew('tmpl.Rnw','doc.Rnw'); knit2pdf('doc.Rnw)

... 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)

推荐答案

我更喜欢使用专用的模板库(例如whiskerbrew)来实现您想要的功能,因为尝试使用R函数恕我直言编写乳胶代码很丑陋.模板文件如下所示,名为tpl.Rnw.您可以通过运行以下命令将其转换为pdf.您可以轻松编写一个函数来封装此逻辑,从而使用knitr将啤酒模板转换为pdf.

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')

模板文件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}

这篇关于带编织物字幕的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:15