Adds a minimal spin preamble to an R script file if one doesn't already exist.
The preamble includes a title derived from the filename and is formatted as
a YAML block suitable preprended with #'
for knitr::spin()
.
Arguments
- script
Path to the R script file
- title
Custom title for the preamble. If provided, overrides any title in the
preamble
list. If NULL, usespreamble$title
or filename as fallback.- preamble
Named list of YAML metadata to include in preamble. The
title
parameter takes precedence overpreamble$title
if both are provided.
Details
This is useful to prepare R scripts for use with Quarto Script rendering support. See https://quarto.org/docs/computations/render-scripts.html#knitr
Preamble format
For a script named analysis.R
, the function adds this preamble by default:
This is the minimal preamble required for Quarto Script rendering, so that Engine Bindings works.
Examples
if (FALSE) { # \dontrun{
# Basic usage with default title
add_spin_preamble("analysis.R")
# Custom title
add_spin_preamble("analysis.R", title = "My Analysis")
# Custom preamble with multiple fields
add_spin_preamble("analysis.R", preamble = list(
title = "Advanced Analysis",
author = "John Doe",
date = Sys.Date(),
format = "html"
))
# Title parameter overrides preamble title
add_spin_preamble("analysis.R",
title = "Final Title", # This takes precedence
preamble = list(
title = "Ignored Title",
author = "John Doe"
)
)
} # }