通过 render_function() 渲染多个参数化的 Rmarkdown 文件失败 [英] Rendering multiple parametrized Rmarkdown files by render_function() fails

查看:60
本文介绍了通过 render_function() 渲染多个参数化的 Rmarkdown 文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

灵感来自@djnavarro 的推文(https://twitter.com/djnavarro/status/1101623527787429a>) 我尝试编写一个函数来在一个命令中呈现多个参数化报告,但是,该函数失败并显示错误,如下面的最小表示:

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:

名为 summary_cyl.Rmd 的 .Rmd 文件

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

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

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

根据 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:

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

render_report(cyl = 6)

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

Trying to use @djnavarro's approach fails:

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

错误信息是:path.expand(path) 错误:'path' 参数无效

我不知道在哪里找到错误:我以前从未使用过 purrr::walk() 并且我不完全确定 render_function()> 在后台执行..Rmd 文件和 .R 文件都位于同一文件夹中.有没有人知道可能是什么问题?

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?

我的 session.info:

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

带有两个参数的新测试:

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

列表:

    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)

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

what does not work but not even give an error:

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

我也尝试过 purrr::walk2() 但没有任何反应 - 我什至没有收到错误消息.

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

推荐答案

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

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)

要避免这种情况,请在 walk 中指定 file 参数:

To avoid this specify file argument inside walk:

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

注意,现在 filewalk 的参数,它将通过 walk 传递给 render_report.

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

这篇关于通过 render_function() 渲染多个参数化的 Rmarkdown 文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆