2

I would like to refer to the YAML frontmatter of my RMarkdown file like this:

---
title: This is my title
author: My Name
output: html_document
---

# Main page

\```{r}
# Of course, the leading backslash is not actually there or at the end of this block

if (output == "html_document") { # How to refer to output (or other items) from YAML?
   outputtype <- "HTML"
} else {
   outputtype <- "Not HTML"
}
\```

The output type is `r outputtype`.

Which should render like:

Main page

The output type is HTML.

I know I can use params but that can be really ugly if I want to refer to multiple items in the header. I would have to do something like this:

---
params:
    loc_title: My Title
    loc_date: 2022-09-02
    loc_output: html_output  # Not even sure I can do this
title: `r params$loc_title`
date: `r params$loc_date`
output: `r params$loc_output`
---

This is ugly so I am hoping there is another way to do this.

dmonder
  • 353
  • 4
  • 12

1 Answers1

2

I found an answer in the question R Markdown - variable output name.

I just needed to use r rmarkdown::metadata$output. Took me a few hours to find a question that had the answer in it so I thought I would post it here.

dmonder
  • 353
  • 4
  • 12