我写了一个参数化报告,它将以两种不同的语言为约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
```
带有小标题,功能和调用的.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文件都位于同一文件夹中。有谁知道可能是什么问题?
我的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
答案 0 :(得分:1)
walk
默认将输入值作为函数的第一个未指定参数传递,因此在您的情况下,它将导致如下调用:
render_report(file = 4)
为避免这种情况,请在file
中指定walk
参数:
param_infos %>%
transpose() %>%
walk(render_report, file = "summary_cyl.Rmd")
请注意,现在file
是walk
的参数,它将由render_report
传递给walk
。