如何在给定一个具有多组输入参数的外部表的情况下多次运行RMarkdown?

jjhzyzn0  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(126)

我有一个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
nwsw7zdq

nwsw7zdq1#

这似乎是工作!注意,前两次我运行这个,它工作(产生报告)的前两个,但抛出一个错误的字符作为日期只为第三个网站。第三次我运行它,错误消失,所有3三个报告生成。

library(readxl)
inputs <- read_excel("X:/DashboardInputs.xlsx")

# Create a report for each row in the inputs table
for (i in seq_along(inputs$site_code)) {
  rmarkdown::render("X:/TemplateMarkdown.Rmd", 
                    output_dir = "S:/Dashboards" ,
                    output_file = sprintf( inputs$site_acr[i]),
                    params = list(
                      site_code  = inputs$site_code[i], 
                      site_acr   = inputs$site_acr[i], 
                      site_full  = inputs$site_full[i] ,
                      start_date = inputs$start_date[i],
                      end_date   = inputs$end_date[i]  ))
}

相关问题