Rendering multiple parametrized Rmarkdown files by render_function() fails

痴心易碎 提交于 2021-02-11 12:41:44

问题


I wrote a parametrized report that will create individual reporting for ~120 centers in two different languages. The reports are created by the knit-button so far (which works perfectly fine).

Inspired by @djnavarro's tweet (https://twitter.com/djnavarro/status/1101623527872970754) I tried writing a function to render multiple parametrized reports in one command, however, the function fails with Error as shown in the minimal reprex below:

The .Rmd-file called summary_cyl.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
```

The .R-file with the tibble, the function and the call:

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

Creating the function as inspired by 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")
  )
}

Calling the function alone works fine, it creates me a PDF with the desired results in the desired folder:

render_report(cyl = 6)

Trying to use @djnavarro's approach fails:

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

The error message is: Error in path.expand(path) : invalid 'path' argument

I don't know where to locate the error: I never used purrr::walk() before and I'm not completely sure about what the render_function() does in the background. Both - the .Rmd-file and the .R file - are located in the same folder. Does anyone have an idea what might be the problem?

my session.info:

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

EDIT: new testing with two params:

---
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
```

the list:

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

the function:

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")
  )
}

the call (works):

render_report(cyl = 4, vs = 1)

what does not work but not even give an error:

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

I also tried purrr::walk2() but nothing happens - I dont even get an error.


回答1:


walk by default passes the input value as the first not specified argument of a function, so in your case it results in call like:

render_report(file = 4)

To avoid this specify file argument inside walk:

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

Note, that now file is argument of walk and it will be passed to render_report by walk.



来源:https://stackoverflow.com/questions/54969070/rendering-multiple-parametrized-rmarkdown-files-by-render-function-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!