Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

r - 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. 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}
itle{Using knit\_expand() for templates}
author{Yihui Xie}
egin{document}

maketitle
ableofcontents

<<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}
itle{Using knit\_expand() for templates}
author{Yihui Xie}
egin{document}

maketitle
ableofcontents

<<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}
itle{Brew + knitR}
author{Ramnath Vaidyanathan}
egin{document}

maketitle
ableofcontents



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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}
itle{Not using knit\_expand() for templates}
author{Yihui Xie}
egin{document}

maketitle
ableofcontents

<<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='
'),
    vars, vars, vars)
@
Sexpr{knit(text = src)}

end{document}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...