我有一个RMarkdown文件,它是用于生成多个报告(每个站点一个)的格式。目前,我要么在Markdown顶部编辑输入,输入给定站点的详细信息,然后运行报告。然后我会重复此操作15次。
相反,我想写一个单独的R脚本,呈现RMarkdown脚本,并为每个站点生成单独的html,其中站点输入通过外部表读入。有什么建议吗?最好的解决方案是包含一个循环来为Excel中的每一行生成它,还是其他什么?代码建议非常感谢!
过去减价:
# Set these parameters only once
start_date <- "2020-04-01"
end_date <- "2022-03-31"
site_acr <- "siteA"
site_code <- 1
要读入R脚本的示例Excel表
site_code site_Acr site_name start_date end_date
1 siteA siteAfull 2020-01-01 2022-12-31
2 siteB siteBfull 2019-01-01 2021-12-31
3 siteC siteCfull 2020-07-01 2022-06-31
编辑:我通过YAML阅读器将Markdown转换为参数化报表,它适用于单个站点:
title: "**Quality Dashboard: `r params$site_full`**"
output:
flexdashboard::flex_dashboard:
orientation: rows
params:
site_acr: siteA
site_full: siteAfull
site_code: 1
start_date: 2020-04-01
end_date: 2022-03-31
rmarkdown::render()etc... I'm now trying to follow this example, where I expect the paramaters from my
输入文件取代了rmarkdown::render()
的多个手动行,以覆盖YAML中的那些行:https://bookdown.org/yihui/rmarkdown-cookbook/parameterized-reports.html
inputs <- structure(list(site_code = c(1, 2, 3),
site_acr = c("siteA", "siteB", "siteC"),
site_name = c("siteAfull ", "siteBfull", "siteCfull"),
start_date = structure(c(1588291200, 1588291200, 1572566400), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
end_date = structure(c(1651276800, 1651276800, 1635638400 ), class = c("POSIXct", "POSIXt"), tzone = "UTC")),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L))
render_one <- function(site_code, site_acr , site_full, start_date, end_date ) {
# assuming the output format of input.Rmd is html
rmarkdown::render(
'X:/filepath/Quality Dash.Rmd',
output_file = paste0(site_acr, '.html'), # Name is file by site
params = list(site_code = site_code,
site_acr = site_acr,
site_full = site_full,
start_date = start_date,
end_date = end_date ),
envir = parent.frame()
)
}
# Not working:
for (site in inputs$site_acr) {
render_one(site_code, site_acr , site_full, start_date, end_date )
}
# Error: pandoc document conversion failed with error 64
1条答案
按热度按时间nwsw7zdq1#
这似乎是工作!注意,前两次我运行这个,它工作(产生报告)的前两个,但抛出一个错误的字符作为日期只为第三个网站。第三次我运行它,错误消失,所有3三个报告生成。