R中带有子组循环的多个自动报告 [英] Multiple Automatic Reports with Subgroup Loop in R
问题描述
我正在尝试根据培训中心城市为健身测试数据创建自动的PDF RMarkdown报告.
I'm trying to create automated PDF RMarkdown reports for fitness testing data based on the training hub city.
我相信按照下面的大纲我会非常接近: R Knitr PDF:是否存在是否可以通过循环自动保存从.Rmd生成的PDF报告?
I believe I'm very close by following the outline here: R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop?
但是,尽管命名不同(report.MTL.pdf和report.TOR.pdf),但这仅为1个集线器创建了具有相同数据的报告.如何使子组正确循环以显示来自不同集线器的数据?
However, this is creating reports with the same data for only 1 hub despite both being named differently (report.MTL.pdf and report.TOR.pdf). How can I get the subgroup to loop properly to show data from the different hubs?
样本数据:
Date Athlete Test Average Hub
1 2019-06-03 Gabel Broad_Jump 175.000000 TOR
2 2019-06-10 Gabel Broad_Jump 187.000000 TOR
3 2019-06-10 Clark Broad_Jump 200.666667 MTL
4 2019-06-10 Pozzebon 10m_Sprint 1.831333 MTL
5 2019-06-10 Clark 10m_Sprint 2.026667 MTL
6 2019-06-17 Gabel Broad_Jump 191.500000 TOR
7 2019-06-17 Clark Broad_Jump 200.666667 MTL
8 2019-06-17 Pozzebon 10m_Sprint 1.803667 MTL
9 2019-06-17 Clark 10m_Sprint 2.090000 MTL
10 2019-06-24 Gabel Broad_Jump 192.000000 TOR
用于渲染RMarkdown的R脚本
R Script for Rendering RMarkdown
NWT <- read.csv("NWT.csv")
for (hub in unique(NWT$Hub)){
subgroup <- subset(NWT, Hub == hub)
render("Hub_Test.rmd",output_file = paste0('report.', hub, '.pdf'))
}
RMarkdown文件(Hub_Test.rmd):
RMarkdown File (Hub_Test.rmd):
NWT <- read.csv("NWT.csv")
for (hub in unique(NWT$Hub)){
subgroup <- subset(NWT, Hub == hub)
}
summary(subgroup)
此设置在我的工作目录中仅使用MTL数据创建2个PDF.我肯定错过了什么.
This set up creates 2 PDFs in my working directory with ONLY MTL data. I must be missing something.
推荐答案
hub
被传递给Hub_Test.rmd
,因此您无需在rmd文件中编写循环.另一个问题是,我相信pandoc在使用summary
写入pdf时会遇到一些问题,因此我在这里使用的是html输出,但是总体思路是相同的.
hub
is passed to Hub_Test.rmd
, so you don't need to write the loop in the rmd file. The other issue is that I believe pandoc has some issues writing to pdf with summary
, so I'm using html output here, but the general idea is the same.
```{r, echo=FALSE}
NWT <- structure(list(Athlete = structure(2:1, .Label = c("Clark", "Gabel"
), class = "factor"), Test = structure(2:1, .Label = c("10m_Sprint",
"Broad_Jump"), class = "factor"), Hub = structure(2:1, .Label = c("MTL",
"TOR"), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
subgroup <- subset(NWT, Hub == hub)
summary(subgroup)
```
生成脚本
library(rmarkdown)
NWT <- structure(list(Athlete = structure(2:1, .Label = c("Clark", "Gabel"
), class = "factor"), Test = structure(2:1, .Label = c("10m_Sprint",
"Broad_Jump"), class = "factor"), Hub = structure(2:1, .Label = c("MTL",
"TOR"), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
for (hub in unique(NWT$Hub)){
subgroup <- subset(NWT, Hub == hub)
render("Hub_Test.rmd",output_file = paste0('report.', hub, '.html'))
}
这篇关于R中带有子组循环的多个自动报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!