Sweave可以自动产生许多PDF吗? [英] Can Sweave produce many pdfs automatically?

查看:77
本文介绍了Sweave可以自动产生许多PDF吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分析了许多城市(数百个)的测量结果,并且需要为每个城市创建单独的报告(Adobe pdf格式).

I analyze measurements from many cities (hundreds), and need to create separate reports per city (in Adobe pdf-format).

我的过程是这样的:

  1. 第一个RStudio准备要显示的数据,保存在X.Rda中.
  2. 在X.Rnw(RStudio)中,我阅读X.Rda,选择一个城市,并生成表格和曲线图.
  3. 在RStudio中,我按编译PDF",然后生成城市报告X.pdf.
  4. 我转到第2步,选择另一个城市,依此类推.

这很繁琐,对于每个城市来说都是很不错的选择,但是如何做到呢?

This is very tedious, and looks perfect for a for-loop per city, but how can it be done?

谢谢r贡献者!

/克里斯

推荐答案

您可以使用诸如for循环这样的方式来更改全局变量,该变量控制要编织到报告中的城市.参见其他文章使用现有R会话中的对象运行Sweave或knitr

You can use something like a for loop with a global variable changing, which controls which city you want to weave into the report; see the other post Run Sweave or knitr with objects from existing R session

代码类似于(假设cities是字符向量,我使用 knitr 包作为示例,因为您可以指定输出的文件名):

The code will be like (suppose cities is a character vector, and I use the knitr package as an example because you can specify the filename of the output):

for (city in cities) {
   knit('city_template.Rnw', output = paste('report_', city, '.tex', sep = ''))
}

city_template.Rnw内部,您有一个类似

<<do-my-job>>=
make_plot(city, ...)
whatever(city, ...)
@

然后,您将获得一系列以城市命名的tex文件,剩下的工作是将它们编译为PDF(RStudio无法编译多个tex文件AFAIK,但是在其中进行此操作很简单.命令行或R中的texi2dvi()).

Then you will get a series of tex files named by the cities, and the rest of your job is to compile them to PDF (not possible for RStudio to compile multiple tex files, AFAIK, but it is trivial to do it in command line or in R with texi2dvi()).

您需要注意一件事-您必须为每个输出文件使用不同的地物前缀(选项fig.path),否则不同的城市可以覆盖彼此的地物输出.在knitr中,可以这样操作:

There is one thing you need to be careful -- you have to use a different figure prefix (the option fig.path) for each output file, otherwise different cities can override each other's figure output. In knitr, this can be done by like this:

<<setup, echo=FALSE>>=
opts_chunk$set(fig.path = paste('my-prefix-', city, sep = ''))
@

我相信这样做可以安全地生成许多带有循环的报告.

I believe this should be safe to produce many reports with a loop.

顺便说一句,您当然可以使用Sweave达到相同的目标;也许您会知道我后来为什么开发knitr的原因(这是题外话,所以我在这里不再赘述).

BTW, you can certainly achieve the same goal with Sweave; perhaps you will know why I developed knitr later (this is off-topic, so I won't expand here).

这篇关于Sweave可以自动产生许多PDF吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆