简体   繁体   中英

Incorporate RMD file in R-package while using non-exported functions

I'm creating an R package which one of its function's output is an HTML report. I want to use an Rmd file to create it, that will be installed within the package.

Following this thread , I understood the usage of inst/rmd/file.Rmd and how to call it ( system.file("rmd", "report.Rmd", package = "thepackage") ).

My package function thepackage::run_report(params, dir) should call the Rmd file, send parameters and eventually export the report to a specified directory.

In the report itself, I'd like to run both exported functions of thepackage AND non-exported functions.

I simplified the code, but it matches the idea.

  • Exported function to run the report:
run_report(params, dir = getwd()) {
  input <- system.file("rmd", "report.Rmd", package = "thepackage")

  rmarkdown::render(input = input,
                    params = params,
                    output_file = "report.html",
                    output_dir = dir,
                    clean = TRUE)
}
  • report.Rmd. get_data() is also an exported function of thepackage:
---
title: "Comparison Report"
output: html_document
params:
  data: ""
  impute_func: ""

``` {r setup}
    knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, cache = FALSE)
    data <- params$data
    impute_func <- params$impute_func
``` #

``` {r get_data}
    library(thepackage)
    dataset <- get_data(data)
    dataset_ready <- thepackage:::impute_values(data, impute_func)
``` #

``` {r summary}
    summary(dataset_ready)
``` #

I'd like to be able to use both the exported and non-exported functions of thepackage, without having to use the::: command. Overall, I'd like the RMD to behave as any other exported function in the package, which can use non-exported functions from the same package.

If known, what is the best practice for such cases?

Best practice is to use only exported functions. Think about your design carefully if you find that you need to use internal functions: won't users of your package also have the same need?

So there is no standard way to do what you want, but there are lots of possibilities for non-standard ways. Probably the simplest is to have a code block at the beginning of the document that imports each of the internal functions you want, eg

```{r echo = FALSE}
impute_values <- thepackage:::impute_values
```

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