I'd like to programmatically add tags to the YAML header of a blogdown post when clicking the "knit" button.
For example something like this:
---
title: This is a post
author: brshallo
date: '2022-02-11'
tags: "`r stringr::str_c('\n - ', stringr::str_flatten(c('rstats', 'datascience'), '\n - '))`"
slug: this-is-a-post
---
I've tried a variety of solutions with !r
, !expr
,
and iterations on these but so far no luck.
Here are some related threads:
I'd at first assumed this was a more general rmarkdown problem rather than blogdown specific but given that some of the solutions mentioned in threads above seemed to work when inputting generic params I wasn't sure...
To generate a valid YAML array, you could use the alternative syntax [ ], e.g.,
tags: ["`r paste(head(letters), collapse = '", "')`"]
which generates:
tags: ["a", "b", "c", "d", "e", "f"]
Note the hack collapse = '", "'
: since there already exists a pair of double quotes outside the R expression, you should only generate the part a", "b", "c", "d", "e", "f
from the R expression.
-- solution copied from Yihui's explanation at blogdown#647