Configure YAML Headers in R Markdown
How the R Markdown YAML header controls title, output format and parameters, which options matter, and how to keep headers valid when copied or shared.
Every R Markdown document can start with a YAML header: a short block of settings between two lines of three dashes. It sets the document title, author and date, chooses the output format, and passes options to the rendering step. This review explains what the header changes, which options are most useful, and where copying, reformatting or sharing a header can break it. No test outputs or measurements were supplied for this article, so the guidance relies on documented, generally verifiable YAML and R Markdown behavior. Gaps are noted where they matter.
What the YAML header actually does
The header is plain text stored at the top of the .Rmd file. When you knit or call rmarkdown::render(), the header is parsed as structured data. R Markdown uses some fields to choose and configure the output format. It passes other fields to Pandoc, which uses them as metadata and template variables. The header does not appear as literal text in the rendered output. Its values only show up where the template places them, such as the title block.
---
title: "Quarterly Summary"
author: "Analytics Team"
date: "2026-10-31"
output: html_document
---
Two layers are involved. Editing the header changes stored characters in the source file. The visible effect, such as a table of contents or a code theme, only appears in the rendered document. A header that looks tidy in the editor can still fail to parse. A messy-looking header can still be valid. Only parsing and rendering show which case you have.
Commonly used header fields
| Field | Purpose | Notes |
| title | Document title shown in the title block and, for HTML, the page title | Quote it if it contains a colon followed by a space, or starts with a special character |
| subtitle | Secondary title line | Support depends on the output template |
| author | One name or a list of names | Use a YAML list for several authors |
| date | Date line in the title block | Can hold inline R code if the whole value is quoted |
| output | Output format and its options | The main control point for rendering |
| params | Named parameters available as params$name in code | Used for parameterized reports |
| abstract | Summary paragraph | Placement depends on the template |
| bibliography / csl | Citation source file and citation style | Processed by Pandoc |
| lang | Document language, such as en-US | Sets the language attribute in HTML output when the template supports it |
Output format options
If you don't need options, the format name can sit directly after output:. To add options, nest them under the format name with consistent indentation and a trailing colon after the format name. This switch from a single value to a nested mapping is the most common point of error.
---
title: "Quarterly Summary"
output:
html_document:
toc: true
toc_depth: 2
toc_float: true
number_sections: true
code_folding: hide
df_print: paged
theme: flatly
highlight: tango
---
- toc and toc_depth: add a table of contents and limit how many heading levels it includes.
- toc_float: keeps the contents panel visible beside the text. This is an html_document option.
- number_sections: numbers headings in the output.
- code_folding: hide or show lets readers expand or collapse code chunks in HTML output.
- df_print: controls how data frames print, for example paged or kable.
- theme and highlight: choose the page theme and the code syntax highlighting style for HTML.
- fig_width, fig_height and fig_caption: set default figure size and caption behavior.
- self_contained: embeds dependencies in a single HTML file, which makes the file easier to share.
To produce several formats, list each one. Use default for formats without options. Options for one format do not carry over to another, so set them separately.
---
output:
html_document:
toc: true
pdf_document:
latex_engine: xelatex
keep_tex: true
word_document: default
---
Parameters and inline R code
The params field declares values that code chunks can read as params$name. You can override them at render time with rmarkdown::render("report.Rmd", params = list(region = "West")). This makes one source file serve several report variants without editing the body.
---
title: "Regional Report"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
params:
region: "East"
year: 2026
output: html_document
---
Inline R in the header is evaluated during rendering, so the date above updates each time the document is knit. The whole value must be quoted. Unquoted, the backticks and inner quotes can be misread by the YAML parser.
What reformatting a header changes
In YAML, whitespace carries meaning. Indentation defines which options belong to which format. Reformatting therefore changes structure, not just appearance. Changing indentation can move an option out of html_document and up to the top level. There it may be ignored or misread, often without an obvious error. Reordering keys within the same level does not change meaning. Changing quoting can: some unquoted words, such as yes and no, may be read as true or false by some YAML parsers. Quote any value you intend as text.
The word 'font' is ambiguous here. A header field such as mainfont tells the PDF engine which installed typeface to use. It does not change the stored characters. That is different from Unicode 'font' generators, which swap ordinary letters for look-alike symbols. Those substituted characters stay in the text and can affect search, sorting and screen reader output. For document styling, use real header or template options rather than character substitution.
Copying and sharing headers
Headers are often copied from forum answers, chat messages and documentation pages. Several problems come from the copy step rather than the settings themselves:
- Tabs: YAML does not allow tabs for indentation. An editor or chat tool that converts spaces to tabs can make the header fail to parse.
- Non-breaking spaces: some web pages and word processors insert non-breaking spaces. They look like normal spaces but are not valid YAML indentation.
- Curly quotes: rich-text tools may turn straight quotes into curly ones. Curly quotes are not YAML quoting characters, so the value is treated as unquoted text, and a colon inside it can then break parsing.
- Lost delimiters: the header must start on the first line of the file with three dashes and end with three dashes. A missing or indented delimiter means the block is treated as ordinary text.
- Collapsed line breaks: pasting into a single-line field joins the lines and destroys the structure.
When sharing a header, send it as plain text or in a code block, not as formatted rich text. The plain-text version is the only one that reliably keeps spacing and quote characters intact. Rendering in one chat tool or editor does not show that every destination handles the characters the same way. No destination-specific tests were supplied for this article.
Readability and accessibility of the output
The header itself is not read by end readers. Its options shape how navigable the rendered document is:
- A table of contents and numbered sections give readers, including those using assistive technology, a structure to move through. This only works if the body uses real Markdown headings.
- The lang field records the document language. In HTML this can help screen readers choose pronunciation, but the effect depends on the template and the reader software.
- Folded code hides chunks behind a control. Check that the content readers need is not only inside collapsed sections.
- Figure captions turned on with fig_caption give images visible descriptions. Alt text is set in the body or chunk options, not in the header.
Behavior with specific screen readers or browsers was not tested for this article. If accessibility is a requirement, check the rendered file with the assistive technology your readers use.
Checking a header before knitting
You can parse the header on its own to catch structural mistakes before a full render. If the returned list does not show options nested under the format you expected, the indentation is wrong.
meta <- rmarkdown::yaml_front_matter("report.Rmd")
str(meta)
# Test a snippet directly
yaml::yaml.load("output:\n html_document:\n toc: true")
R Markdown headers versus Quarto headers
Quarto documents also start with a YAML header, but they mostly use format: where R Markdown uses output:. Some option names differ as well. A header copied between the two systems may parse as valid YAML but configure nothing. Check which system will render the file before reusing a header.
Is a YAML header the right choice?
For any document rendered with R Markdown, yes. The header is the intended place to set metadata, output formats and parameters. It keeps those settings with the source, so the document renders the same way each time. The main tradeoff is fragility: meaning depends on exact spacing and quoting, and small edits or a careless paste can silently change the result.
Plain text without a header is the better fit in a few cases. One is when the file will be viewed in a general Markdown viewer that does not process R Markdown, which may show the block as raw text or handle it differently. Another is when you only need to share notes, not a rendered report. The last is when you are pasting settings into a tool that changes whitespace or quotes, where a plain-text code block is the safe way to pass the header along.
Evidence gaps
The available material held no specific measurements, version numbers, test renderings or tool comparisons. This article does not state which R Markdown or Pandoc versions support each option, how particular editors handle pasted headers, or how specific assistive technologies read the output. Check those points in your own environment and against the current package documentation.