R Knitr PDF:是否有可能通过循环自动保存 PDF 报告(从 .Rmd 生成)? [英] R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop?
问题描述
我想创建一个循环,它允许我自动保存从 .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 报告...需要一点帮助才能让我克服困难) 已经处理了从 .Rnw 生成的多个 pdf 报告的创建文件.我尝试按如下方式应用该方法:
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
- 定义模板内部使用的
subgroup
对象 - 将模板渲染为所需的输出
所以,在这个单独的脚本中:
# 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 中的knit"按钮,这将不起作用,因为它会在单独的 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.
不是依赖全局变量,另一种选择是使用参数化报告,在 YAML 头中定义参数,并将参数值作为参数传递给 rmarkdown::render
.
Rather than relying on global variables, another option would be to use parametrized reports, defining parameters in the YAML header, and passing the parameter values in as arguments to rmarkdown::render
.
这篇关于R Knitr PDF:是否有可能通过循环自动保存 PDF 报告(从 .Rmd 生成)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!