How to Stop Duplicating Rmd Template Assets within Packages and Projects?

I have a package with numerous Rmd templates created by usethis::use_rmarkdown_template. Within the YAML of each template I specify various 'includes', e.g. css, html header and footer, etc. Everything works great, but I am re-duplicating all the same assets within each inst/rmarkdown/templates/.../skeleton/. Seems inefficient. Furthermore, these same assets are copied to new project directories when users create new projects/reports. Again, everything works fine in this setup, but we end up with now dozens of copies of the same css, html, and image assets both within the package and on various file servers. How do I best cut down on all this re-duplication?

1 Like

One idea would be to create custom output functions and distribute them via a package. Then when a user wants to create a report using that template, they could specify that output format function in the YAML header.

As a hypothetical example, if you created a package called rmdTemplates that contained the custom output format function customTemplate(), then a user could use that template by installing rmdTemplates and specifying the following YAML header:

---
output: rmdTemplates::customTemplate
---

Using this strategy, all the assets can be defined once inside the package. Then the related templates can either separately import these asset files, or they could extend each other (e.g. a base format baseTemplate() could import all the shared assets, and then the various other templates would extend baseTemplate()).

1 Like

I think this is precisely what I need! Do I need to have the accompanying includes in the YAML? E.g.:

---
title: "Report Title"
date: '`r format(Sys.Date(), "%B %d, %Y")`'
output: 
  rmdTemplates::customTemplate:
    css: style.css
    toc: true
    includes:
      before_body: header.html
      after_body: footer.html
---

Or does that get specified in customTemplate(...)?

1 Like

That's your choice. It's highly customizable. Based on the description in your original post, I think you would want to specify it in customTemplate(). Then your users will only need to specify additional arguments if they want to further customize their document (e.g. enhance or override your default settings).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.