简体   繁体   中英

developing R package with templete .Rmd files findable to package function

I'm developing an R package, one of my package function is generate_report() which generate an html report with rmarkdown using a templete Rmd file and function arguments:

#' generate report based on templete file
#' @import rmarkdown
#' @export
generate_report <- function(x, y){
  rmarkdown::render('templete.Rmd', envir = list(x = x, y = y))
}

and here is the inst/templete.Rmd file: (when the package is compiled, it will be moved to top-level folder of package:

---
title: "templete"
output: html_document
---

## Head 1

```{r}
print(x)
```

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

my question is, when the package is devtools::install() ed, function generate_report() can not find file templete.Rmd , how to make the function find this templete.Rmd file in right way ?

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

system.file is the right way, thanks @MrFlick and @Jonathan Carroll. This is my final code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM