There’s been some recent commotion about automating Rmarkdown reports. And surprisingly, there isn’t (yet) an RStudio-sanctioned method of doing so. We’ve been left to our own devices, at least for the moment.

Here I’ll share our office’s approach to automated Rmarkdown reporting via the funcreporter package. It’s not the only approach, and it might not even be the best one, but it works. The infrastructure is mostly in place via rmarkdown, usethis, and devtools; we only had to put it all together.

Create your Reporting Package

First, create a package that contains your RMarkdown reports, parameterized or not. I won’t go over package development here, but adding templates to a package is trivial: use usethis::use_rmarkdown_template and you’re good to go. Follow the mechanics of parameterized reporting here.

Before we get to funcreporter, there’s a little demo package called funcreports to give you an idea of what a reporting package looks like. If you want to follow along, install it with:

remotes::install_github("daranzolin/funcreports")

Within funcreports, note how each RMarkdown template has a skeleton.Rmd in a separte folder in the directory tree:

├── DESCRIPTION
├── LICENSE
├── LICENSE.md
├── NAMESPACE
├── R
│   └── hello.R
├── README.md
├── funcreports.Rproj
├── inst
│   └── rmarkdown
│       └── templates
│           ├── sample-1
│           │   ├── skeleton
│           │   │   └── skeleton.Rmd
│           │   └── template.yaml
│           ├── sample-2
│           │   ├── skeleton
│           │   │   └── skeleton.Rmd
│           │   └── template.yaml
│           └── sample-3
│               ├── skeleton
│               │   └── skeleton.Rmd
│               └── template.yaml

Install and set up funcreporter

After you’ve created your reporting package (or installed funcreports), install funcreporter:

remotes::install_github("ir-sfsu/funcreporter")

funcreporter is really just a wrapper that coordinates some logistics between your .Renviron, .libPaths, rmarkdown::render, and here::here. But first it has to know which package to use for reporting:

library(funcreporter)
set_funcreporter_pkg("funcreports")

Setting env var FUNCREPORTER_PKG=funcreports
Setting env var FUNCREPORTER_PATH_TO_TEMPLATES=/Library/Frameworks/R.framework/Versions/4.0/Resources/library/funcreports/rmarkdown/templates

funcreport!

Now you–the user–just need to know the names of your reports and their associated parameters. Here I’ll run the “Sample 2” report you can see in the tree directory above (the .yaml file title):

funcreporter(
  template_name = "Sample 2", 
  output_file = "carb-report",
  params = list(cyl = 6)
)

Nothing special in and of itself. But funcreporter’s params argument can take list values of length greater than 1, meaning you can render any number of reports with a single function call. Here I’ll run the “Sample 1” report for all three iris species, setting an additional breaks parameter to 15 and adjusting a plot title accordingly:

species <- unique(iris$Species)
funcreporter(
  template_name = "Sample 1",
  output_file = paste0(species, "-report"),
  params = list(species = species, breaks = 15, plot_title = paste(species, "Distribution"))
)

Now that’s power! Three reports for three species isn’t much, but how about 100 reports for 100 clients?

funcreporter

Give funcreporter a whirl, and let me know if it works.