【问题标题】:developing R package with templete .Rmd files findable to package function使用模板开发 R 包 .Rmd 文件可找到以打包功能
【发布时间】:2017-07-07 04:35:52
【问题描述】:

我正在开发一个 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}
print(x)
```

```{r}
print(y)
```

我的问题是,当包是devtools::install()ed时,函数generate_report()找不到文件templete.Rmd,如何让函数以正确的方式找到这个templete.Rmd文件?

【问题讨论】:

标签: r r-markdown r-package


【解决方案1】:

您的rmarkdown::render() 呼叫需要按照http://r-pkgs.had.co.nz/inst.html 使用system.file

【讨论】:

  • 在@MrFlick 的评论中也同时提到。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 2014-09-04
    • 2015-12-26
    • 2021-12-02
    • 2021-09-20
    • 2011-12-08
    相关资源
    最近更新 更多