# 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
format_front_matter(doc)
# Write to a file
tmp <- tempfile(fileext = ".md")
write_front_matter(doc, tmp)
readLines(tmp)
# Print to console (when path is NULL)
write_front_matter(doc)
# Use TOML format
format_front_matter(doc, delimiter = "toml")
# Use comment-wrapped format for R scripts
r_script <- list(
data = list(title = "Analysis Script"),
body = "# Load libraries\nlibrary(dplyr)"
)
format_front_matter(r_script, delimiter = "yaml_comment")
# Roundtrip example: read, modify, write
original <- "---
title: Original
---
Content here"
doc <- parse_front_matter(original)
doc$data$title <- "Modified"
format_front_matter(doc)
Run the code above in your browser using DataLab