How to load the same needed packages in multiple blogdown posts?

Viewed 54

In some but not all posts of a blog made with blogdown I use highcharts to create interactive charts. For this, I need:

  1. Include the necessary highcharts javascript sources in the relevant posts
  2. Load the highcharter package (and also magrittr, as they work well together) in the relevant .Rmds

For the first point, a fairly elegant solution may be including a new param usehighcharts: true in the params of the posts and then in the header.html partial use:

{{ if .Params.usehighcharts }}
 ... include the needed javascript ...
{{ end }}

But for the second point, I do not have an elegant solution, so what I do is manually include a chunk like so in the beginning of the relevant .Rmds, which I find error prone and not very elegant:

```{r echo=FALSE}
suppressPackageStartupMessages({
  library(highcharter)
  library(magrittr)
})
```

What would be a more elegant/best practice solution to this ?

0 Answers
Related