R Knitr PDF:是否有可能通过循环自动保存PDF报告(从.Rmd生成)? [英] R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop?

查看:730
本文介绍了R Knitr PDF:是否有可能通过循环自动保存PDF报告(从.Rmd生成)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个循环,它允许我自动保存从 .Rmd 文件生成的PDF报告。例如,如果一个变量ID有10行,我希望R自动保存我的10个报告到一个特定的目录。这些报告将根据所选的ID而有所不同。

I would like to create a loop, which allows me to automatically save PDF reports, which were generated from a .Rmd file. For instance, if a variable "ID" has 10 rows, I would like R to automatically save me 10 reports, into a specific directory. These reports shall vary based on the ID selected.

以前的帖子(使用带有knitr的循环来生成多个pdf报告...需要一点帮助才能使我越过hump )已经处理了多个pdf的创建从 .Rnw 文件生成的报告。我试图应用该方法如下:

A previous post (Using loops with knitr to produce multiple pdf reports... need a little help to get me over the hump) has dealt with the creation of multiple pdf reports generated from .Rnw files. I tried to apply the approach as follows:

#Data

```{r, include=FALSE}
set.seed(500)
Score <- rnorm(40, 100, 15)
Criteria1<-rnorm(40, 10, 5)
Criteria2<-rnorm(40, 20, 5)
ID <- sample(1:1000,8,replace=T)
df <- data.frame(ID,Score,Criteria1,Criteria2)

#instead of manually choosing the ID:

subgroup<- subset(df, ID==1) 

# I would like to subset the Data through a loop. My approach was like like this:

for (id in unique(df$ID)){
subgroup<- df[df$ID == id,]}

```

```{r, echo=FALSE}
#Report Analysis

summary(subgroup)
```
#Here will be some text about the summary.



# At the end the goal is to produce automatic pdf reports with the ID name as a filename:

library("rmarkdown")
render("Automated_Report.rmd",output_file = paste('report.', id, '.pdf', sep=''))


推荐答案

适应你的例子:

你需要一个 .rmd 模板文件。它可以是这样的,保存为 template.rmd

You need one .rmd "template" file. It could be something like this, save it as template.rmd.

This is a subgroup report.

```{r, echo=FALSE}
#Report Analysis
summary(subgroup)
```

然后,您需要一个R脚本,将加载所需的数据,循环遍历数据子集以及每个子集

Then, you need an R script that will load the data you want, loop through the data subsets, and for each subset


  1. 定义模板中使用的子组对象

  2. 渲染

所以,在这个单独的脚本中:

# load data 
set.seed(500)
Score <- rnorm(40, 100, 15)
Criteria1<-rnorm(40, 10, 5)
Criteria2<-rnorm(40, 20, 5)
ID <- sample(1:1000,8,replace=T)
df <- data.frame(ID,Score,Criteria1,Criteria2)

library("rmarkdown")

# in a single for loop
#  1. define subgroup
#  2. render output
for (id in unique(df$ID)){
    subgroup <- df[df$ID == id,]
    render("template.rmd",output_file = paste0('report.', id, '.html'))    
}

这在我的工作目录中产生了8个html文件,每个文件都有不同的数据子集。

This produced 8 html files in my working directory, each with a summary of a different subset of the data.

请注意,如果您尝试点击RStudio内的编织按钮,那么不起作用,因为在单独的R中运行R代码会话。但是,当您使用 render (或 knit2pdf )从控制台运行时,rmd文件中的R代码仍然具有访问全球环境。

Note that this will not work if you try clicking the "knit" button inside RStudio, as that runs the R code in a separate R session. However, when you run from the console explicitly using render (or knit2pdf) the R code in the rmd file still has access to the global environment.

实现此目标的另一个选项是使用 brew 包。

A different option for accomplishing this goal would be to use the brew package.

这篇关于R Knitr PDF:是否有可能通过循环自动保存PDF报告(从.Rmd生成)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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