我正在开发一个R包,我的包函数之一是generate_report(),它使用模板Rmd文件和函数参数生成一个带有rmarkdown的html报告:
#' generate report based on templete file
#' @import rmarkdown
#' @export
generate_report <- function(x, y){
rmarkdown::render('templete.Rmd', envir = list(x = x, y = y))
}下面是inst/templete.Rmd文件:(编译包后,它将被移到包的顶层文件夹中:
---
title: "templete"
output: html_document
---
## Head 1
```{r}打印(X)
```{r}打印(Y)
我的问题是,当包被devtools::install()编辑时,generate_report()函数找不到templete.Rmd文件,如何让函数正确地找到这个templete.Rmd文件呢?
发布于 2017-07-07 13:00:07
您的rmarkdown::render()调用需要按照http://r-pkgs.had.co.nz/inst.html使用system.file
发布于 2017-07-07 13:36:59
system.file是正确的方式,感谢MrFlick先生和Jonathan Carroll。这是我的最终代码:
generate_report <- function(x, y, output_dir){
file <- system.file("templete.Rmd", package = 'mypackage-name')
if (missing(output_dir)) {
output_dir <- getwd()
}
rmarkdown::render(file, envir = list(x = x, y = y), output_dir = output_dir)
}https://stackoverflow.com/questions/44962621
复制相似问题