Serialize R data as YAML or TOML front matter and combine it with document
content. format_front_matter() returns the formatted document as a string,
while write_front_matter() writes it to a file or prints to the console.
These functions are the inverse of parse_front_matter() and
read_front_matter().
Usage
format_front_matter(
x,
delimiter = NULL,
format = "auto",
format_yaml = NULL,
format_toml = NULL
)
write_front_matter(
x,
path = NULL,
delimiter = NULL,
...,
format = "auto",
format_yaml = NULL,
format_toml = NULL
)Arguments
- x
A list with
dataandbodyelements, typically as returned byparse_front_matter()orread_front_matter(). Thedataelement contains the metadata to serialize (can beNULLto write body only), andbodycontains the document content (can beNULLor empty).- delimiter
A character string specifying the fence style, or a character vector for custom delimiters. See Delimiter Formats for available options. When
NULL(the default), the delimiter is inferred automatically: ifxwas returned byparse_front_matter()orread_front_matter(), the original fence style is preserved; otherwisewrite_front_matter()falls back to the file extension ofpath, and finally to"yaml".- format
The serialization format:
"auto"(detect from delimiter),"yaml", or"toml". Usually auto-detection works well.- format_yaml, format_toml
Custom formatter functions, or
NULLto use defaults. Each function should accept an R object and return a character string.- path
File path to write to, or
NULLto print to the console- ...
Additional arguments passed to
writeBin()when writing to a file (e.g.,useBytes).
Value
format_front_matter(): A character string containing the formatted document with front matter.write_front_matter(): Called for its side effect; returnsNULLinvisibly.
Functions
format_front_matter(): Format front matter as a stringwrite_front_matter(): Write front matter to a file or console
Delimiter Formats
The delimiter argument controls the fence style used to wrap the front
matter. You can use these built-in shortcuts:
| Shortcut | Format | Opening | Closing | Use Case |
"yaml" | YAML | --- | --- | Markdown, R Markdown, Quarto |
"toml" | TOML | +++ | +++ | Hugo, some static site generators |
"yaml_comment" | YAML | # --- | # --- | R scripts, Python scripts |
"toml_comment" | TOML | # +++ | # +++ | R scripts, Python scripts |
"yaml_roxy" | YAML | #' --- | #' --- | Roxygen2 documentation |
"toml_roxy" | TOML | #' +++ | #' +++ | Roxygen2 documentation |
"toml_pep723" | TOML | # /// script | # /// | Python PEP 723 inline metadata |
"yaml_sql_line" | YAML | -- --- | -- --- | SQL scripts (line comments) |
"toml_sql_line" | TOML | -- +++ | -- +++ | SQL scripts (line comments) |
"yaml_sql_block_compact" | YAML | /* --- | --- */ | SQL scripts (block comments) |
"toml_sql_block_compact" | TOML | /* +++ | +++ */ | SQL scripts (block comments) |
"yaml_sql_block_expanded" | YAML | /* + --- | --- + */ | SQL scripts (block comments) |
"toml_sql_block_expanded" | TOML | /* + +++ | +++ + */ | SQL scripts (block comments) |
For custom delimiters, pass a character vector of length 1, 2, or 3:
Length 1: Used as both opener and closer, with no line prefix
Length 2:
c(opener, prefix)where opener is also used as closerLength 3:
c(opener, prefix, closer)for full control
Custom Formatters
By default, the package uses yaml12::format_yaml() for YAML and
tomledit::to_toml() for TOML. You can provide custom formatter functions
via format_yaml and format_toml to override these defaults.
Custom formatters must accept an R object and return a character string containing the serialized content.
YAML Specification Version
The default YAML formatter uses YAML 1.2 via yaml12::format_yaml(). To use
YAML 1.1 formatting instead (via yaml::as.yaml()), set either:
The R option
frontmatter.serialize_yaml.specto"1.1"The environment variable
FRONTMATTER_SERIALIZE_YAML_SPECto"1.1"
The option takes precedence over the environment variable. Valid values are
"1.1" and "1.2" (the default).
Roundtrip Support
Documents formatted with these functions can be read back with
parse_front_matter() or read_front_matter(). For comment-prefixed
formats (like yaml_comment, yaml_roxy, or yaml_sql_line), a separator
line is automatically inserted between the closing fence and the body when
the body starts with the same comment prefix, ensuring clean roundtrip
behavior.
When delimiter is NULL (the default), the delimiter is inferred
automatically, making roundtrips seamless:
format_front_matter()uses thefence_typeattribute preserved byparse_front_matter()andread_front_matter(), so the output uses the same fence style as the original document.write_front_matter()additionally falls back to a built-in extension-to-delimiter map when the input has nofence_typeattribute:
| Extension | Default delimiter |
.sql | "yaml_sql_block_compact" |
.py | "toml_pep723" |
.R | "yaml_roxy" |
.md, .qmd, .Rmd | "yaml" |
See also
parse_front_matter() and read_front_matter() for the inverse
operations.
Examples
# Create a document with YAML front matter
doc <- list(
data = list(title = "My Document", author = "Jane Doe"),
body = "Document content goes here."
)
# Format as a string (delimiter inferred from fence_type attr, falls back to yaml)
format_front_matter(doc)
#> [1] "---\ntitle: My Document\nauthor: Jane Doe\n---\n\nDocument content goes here.\n"
# Write to a file (delimiter inferred from .md extension -> yaml)
tmp <- tempfile(fileext = ".md")
write_front_matter(doc, tmp)
readLines(tmp)
#> [1] "---" "title: My Document"
#> [3] "author: Jane Doe" "---"
#> [5] "" "Document content goes here."
# Print to console (when path is NULL)
write_front_matter(doc)
#> ---
#> title: My Document
#> author: Jane Doe
#> ---
#>
#> Document content goes here.
# Use TOML format explicitly
format_front_matter(doc, delimiter = "toml")
#> [1] "+++\ntitle = \"My Document\"\nauthor = \"Jane Doe\"\n+++\n\nDocument content goes here.\n"
# Use comment-wrapped format for R scripts explicitly
r_script <- list(
data = list(title = "Analysis Script"),
body = "# Load libraries\nlibrary(dplyr)"
)
format_front_matter(r_script, delimiter = "yaml_comment")
#> [1] "# ---\n# title: Analysis Script\n# ---\n#\n# Load libraries\nlibrary(dplyr)\n"
# Write to an R file: delimiter inferred from .R extension -> yaml_roxy
tmp_r <- tempfile(fileext = ".R")
write_front_matter(r_script, tmp_r)
readLines(tmp_r)
#> [1] "#' ---" "#' title: Analysis Script"
#> [3] "#' ---" ""
#> [5] "# Load libraries" "library(dplyr)"
# Roundtrip: delimiter is automatically preserved from the original format
original <- "# ---
# title: Original
# ---
# R code here"
doc <- parse_front_matter(original)
doc$data$title <- "Modified"
format_front_matter(doc) # uses yaml_comment, matching the source
#> [1] "# ---\n# title: Modified\n# ---\n#\n# R code here\n"
