How can I read/parse the metadata from an rmarkdown file?

I think you are looking for rmarkdown::yaml_front_matter function.

Here is how it works in your example

content <- c(
  "---",
  "title: Leaflet Demo",
  "output: html_document",
  "params:",
  "  tiles:",
  "    label: Tiles",
  "    value: normal",
  "    input: select",
  "    choices:",
  "      - normal",
  "      - monochrome",
  "---",
  "\n",
  "This is a test document")
rmd_file <- tempfile(fileext = ".Rmd")
xfun::write_utf8(content, rmd_file)
yml_metadata <- rmarkdown::yaml_front_matter(rmd_file)
params <- yml_metadata[["params"]]
str(params)
#> List of 1
#>  $ tiles:List of 4
#>   ..$ label  : chr "Tiles"
#>   ..$ value  : chr "normal"
#>   ..$ input  : chr "select"
#>   ..$ choices: chr [1:2] "normal" "monochrome"
unlink(rmd_file)

Created on 2019-12-22 by the reprex package (v0.3.0.9001)

Also, if you want to play with yaml header, there is this new package that may be useful.

Hope it helps.

2 Likes