在RMarkdown中编织时,所有打印在所有pdf中,而不是在每个pdf中打印相应的打印
我正在尝试自动生成每个季度左右必须生成的pdf报告。以下是我的RMarkdown模板(template.Rmd)的示例:在RMarkdown中编织时,所有打印在所有pdf中,而不是在每个pdf中打印相应的打印,r,pdf,dynamic,report,r-markdown,R,Pdf,Dynamic,Report,R Markdown,我正在尝试自动生成每个季度左右必须生成的pdf报告。以下是我的RMarkdown模板(template.Rmd)的示例: --- 标题:“数据报告” 输出: pdf\U文件: 图(小标题)否 字幕:第四季度报告 --- ###这只是我计算代码的一个示例 图1。转院 ```{r,echo=FALSE,fig.width=12,fig.height=6} 图书馆(GG2) Facility.graph我可以通过修复我的Rmarkdown模板来解决我的问题: --- title: "Data Repo
---
标题:“数据报告”
输出:
pdf\U文件:
图(小标题)否
字幕:第四季度报告
---
###这只是我计算代码的一个示例
图1。转院
```{r,echo=FALSE,fig.width=12,fig.height=6}
图书馆(GG2)
Facility.graph我可以通过修复我的Rmarkdown模板来解决我的问题:
---
title: "Data Report"
output:
pdf_document:
fig_caption: no
subtitle: Quarter 4 Report
---
###This is just a sample of my code for the calculation
Figure 1. Transfers to Hospital
```{r, echo=FALSE, fig.width=12, fig.height=6, fig.keep='first'}
library(ggplot2)
Facility.graph <- function(df, na.rm = TRUE, ...){ #Function to create plots
# list of hospitals in data to loop over
Facility_list <- unique(byfacility_graph_data$Facility)
for (i in seq_along(Facility_list)) { # for loop to produce ggplot2 graphs
# create plot for each hospital in df
plots1 <- ggplot(df, aes(Date, y = get(names(df)[3]), group = Facility, colour = Facility)) + geom_point(size=4) + labs(x="", y="Number of Transfers")
print(plots1)
}
}
Facility.graph(df) #Calling the function
```
---
标题:“数据报告”
输出:
pdf\U文件:
图(小标题)否
字幕:第四季度报告
---
###这只是我计算代码的一个示例
图1。转院
```{r,echo=FALSE,fig.width=12,fig.height=6,fig.keep='first'}
图书馆(GG2)
Facility.graph它确实是你想要的。。。在.Rmd中确实有一个循环。您需要在降价报告中设置一个参数,以指定设施当前在范围内的位置,并使用此参数筛选数据。看看@EricLecoutre谢谢你的回复。我可以通过修改代码来解决我的问题。我不必设置参数。
# The R code for knitting the above template.Rmd script to create a pdf document for each hospital with corresponding graphs
library(knitr)
Facility_list <- unique(byfacility_graph_data$Facility)
setwd("C:\\User\\Desktop\\reports")
# create for loop to produce ggplot2 graphs
for (i in Facility_list){
rmarkdown::render(input = "template.Rmd", output_format = "pdf_document", output_file = paste0("reports", i, ".pdf", sep=''))
}
---
title: "Data Report"
output:
pdf_document:
fig_caption: no
subtitle: Quarter 4 Report
---
###This is just a sample of my code for the calculation
Figure 1. Transfers to Hospital
```{r, echo=FALSE, fig.width=12, fig.height=6, fig.keep='first'}
library(ggplot2)
Facility.graph <- function(df, na.rm = TRUE, ...){ #Function to create plots
# list of hospitals in data to loop over
Facility_list <- unique(byfacility_graph_data$Facility)
for (i in seq_along(Facility_list)) { # for loop to produce ggplot2 graphs
# create plot for each hospital in df
plots1 <- ggplot(df, aes(Date, y = get(names(df)[3]), group = Facility, colour = Facility)) + geom_point(size=4) + labs(x="", y="Number of Transfers")
print(plots1)
}
}
Facility.graph(df) #Calling the function
```
library(knitr)
Facility_list <- unique(byfacility_graph_data$Facility)
setwd("C:\\Users\\Desktop")
for (j in unique(byfacility_graph_data$Facility)){
df<-byfacility_graph_data[which(byfacility_graph_data$Facility==j|byfacility_graph_data$Facility=="Average"), 1:3]
rmarkdown::render(input = "template.Rmd",output_format = "pdf_document", output_file = paste0("reports_ ", j, ".pdf", sep=""))
}