首页 > 解决方案 > 通过 render_function() 渲染多个参数化的 Rmarkdown 文件失败

问题描述

我写了一份参数化报告,它将为大约 120 个中心以两种不同的语言创建单独的报告。到目前为止,报告是由编织按钮创建的(效果很好)。

受@djnavarro 的推文 ( https://twitter.com/djnavarro/status/1101623527872970754 ) 的启发,我尝试编写一个函数来在一个命令中呈现多个参数化报告,但是,该函数失败并出现错误,如下面的最小表示所示:

名为 summary_cyl.Rmd 的 .Rmd 文件

---
output: pdf_document
params:
   cyl: 4
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```

## R Markdown - report for cyl == `r params$cyl`

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
mtcars %>% 
  filter(cyl == params$cyl) %>% 
  count
```

带有 tibble、函数和调用的 .R 文件:

library(tidyverse)
# setting up the tibble with all the param values
param_infos <- tibble(cyl = c(4, 6, 8))

根据https://bookdown.org/yihui/rmarkdown/params-knit.html#knit-with-custom-parameters的启发创建函数:

render_report <- function(file = "summary_cyl.Rmd", cyl) {
  rmarkdown::render(file, params = list(
    cyl = cyl
  ), envir = new.env(),
  output_file = paste0("summary-", cyl, "-", ".pdf")
  )
}

单独调用该函数可以正常工作,它会在所需文件夹中为我创建一个带有所需结果的 PDF:

render_report(cyl = 6)

尝试使用@djnavarro 的方法失败:

param_infos %>% 
  transpose() %>% 
  walk(render_report)

错误信息是: Error in path.expand(path) : invalid 'path' argument

我不知道在哪里找到错误:我以前从未使用purrr::walk()过,而且我不完全确定render_function()它在后台做了什么。.Rmd 文件和 .R 文件两者都位于同一个文件夹中。有谁知道可能是什么问题?

我的会话信息:

R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.3

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] forcats_0.4.0      stringr_1.4.0      dplyr_0.8.0.1      purrr_0.3.0        readr_1.3.1       
[6] tidyr_0.8.2        tibble_2.0.99.9000 ggplot2_3.1.0      tidyverse_1.2.1   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0.1      cellranger_1.1.0  pillar_1.3.1.9000 compiler_3.5.2    plyr_1.8.4       
 [6] tools_3.5.2       digest_0.6.18     packrat_0.5.0     lubridate_1.7.4   jsonlite_1.6     
[11] evaluate_0.13     nlme_3.1-137      gtable_0.2.0      lattice_0.20-38   pkgconfig_2.0.2  
[16] rlang_0.3.1       cli_1.0.1         rstudioapi_0.9.0  yaml_2.2.0        haven_2.1.0      
[21] xfun_0.5          withr_2.1.2       xml2_1.2.0        httr_1.4.0        knitr_1.21       
[26] hms_0.4.2         generics_0.0.2    grid_3.5.2        tidyselect_0.2.5  glue_1.3.0.9000  
[31] R6_2.4.0          fansi_0.4.0       readxl_1.3.0      rmarkdown_1.11    modelr_0.1.4     
[36] magrittr_1.5      backports_1.1.3   scales_1.0.0      htmltools_0.3.6   rvest_0.3.2      
[41] assertthat_0.2.0  colorspace_1.4-0  tinytex_0.10      utf8_1.1.4        stringi_1.3.1    
[46] lazyeval_0.2.1    munsell_0.5.0     broom_0.5.1       crayon_1.3.4

编辑:带有两个参数的新测试:

---
output: pdf_document
params:
   cyl: 4
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```

## R Markdown - report for cyl == `r params$cyl`

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
mtcars %>% 
  filter(cyl == params$cyl) %>% 
  count
```

名单:

    library(tidyverse)
# setting up the tibble with all the param values
param_infos <- tibble(cyl = c(4, 6, 8), vs = c(0, 0, 1))

功能:

render_report <- function(file = "summary_cyl.Rmd", cyl, vs) {
  rmarkdown::render(file, params = list(
    cyl = cyl,
    vs = vs
  ), envir = new.env(),
  output_file = paste0("summary-", cyl, vs, "-", ".pdf")
  )
}

通话(有效):

render_report(cyl = 4, vs = 1)

什么不起作用但甚至没有给出错误:

param_infos %>% 
  transpose() %>% 
  walk(render_report, "summary_cyl.Rmd")

我也尝试过purrr::walk2(),但没有任何反应——我什至没有收到错误。

标签: rr-markdownpurrr

解决方案


walk默认情况下,将输入值作为函数的第一个未指定参数传递,因此在您的情况下,它会导致调用如下:

render_report(file = 4)

为避免file在内部指定参数walk

param_infos %>% 
  transpose() %>% 
  walk(render_report, file = "summary_cyl.Rmd")

请注意,那现在file是 of 的参数,walk它将被传递给render_reportby walk


推荐阅读