99# ' See <https://quarto.org/docs/computations/render-scripts.html#knitr>
1010# '
1111# ' @section Preamble format:
12- # ' For a script named `analysis.R`, the function adds this preamble:
12+ # ' For a script named `analysis.R`, the function adds this preamble by default :
1313# ' ```
1414# ' #' ---
1515# ' #' title: analysis
2323# ' [Engine Bindings](https://quarto.org/docs/computations/execution-options.html#engine-binding) works.
2424# '
2525# ' @param script Path to the R script file
26+ # ' @param title Custom title for the preamble. If provided, overrides any title
27+ # ' in the `preamble` list. If NULL, uses `preamble$title` or filename as fallback.
28+ # ' @param preamble Named list of YAML metadata to include in preamble.
29+ # ' The `title` parameter takes precedence over `preamble$title` if both are provided.
2630# ' @return Invisibly returns the script path if modified, otherwise invisible NULL
31+ # '
32+ # ' @examples
33+ # ' \dontrun{
34+ # ' # Basic usage with default title
35+ # ' add_spin_preamble("analysis.R")
36+ # '
37+ # ' # Custom title
38+ # ' add_spin_preamble("analysis.R", title = "My Analysis")
39+ # '
40+ # ' # Custom preamble with multiple fields
41+ # ' add_spin_preamble("analysis.R", preamble = list(
42+ # ' title = "Advanced Analysis",
43+ # ' author = "John Doe",
44+ # ' date = Sys.Date(),
45+ # ' format = "html"
46+ # ' ))
47+ # '
48+ # ' # Title parameter overrides preamble title
49+ # ' add_spin_preamble("analysis.R",
50+ # ' title = "Final Title", # This takes precedence
51+ # ' preamble = list(
52+ # ' title = "Ignored Title",
53+ # ' author = "John Doe"
54+ # ' )
55+ # ' )
56+ # ' }
2757# ' @export
28- add_spin_preamble <- function (script ) {
58+ add_spin_preamble <- function (script , title = NULL , preamble = NULL ) {
2959 if (! fs :: file_exists(script )) {
3060 cli :: cli_abort(c(
3161 " File {.file {script}} does not exist." ,
3262 " Please provide a valid file path."
3363 ))
3464 }
65+
3566 content <- xfun :: read_utf8(script )
3667
3768 # if files starts with a spin preamble, do nothing
@@ -42,13 +73,32 @@ add_spin_preamble <- function(script) {
4273 ))
4374 return (invisible ())
4475 }
45- # prepend the spin preamble
46- filename <- fs :: path_file(fs :: path_ext_remove(script ))
47- preamble <- paste(
76+
77+ # Build preamble metadata
78+ metadata <- list ()
79+
80+ # Start with preamble list if provided
81+ if (! is.null(preamble )) {
82+ if (! is.list(preamble )) {
83+ cli :: cli_abort(" `preamble` must be a named list." )
84+ }
85+ metadata <- preamble
86+ }
87+
88+ # Add or override title
89+ if (! is.null(title )) {
90+ metadata $ title <- title
91+ } else if (is.null(metadata $ title )) {
92+ # Use filename as default title if none provided
93+ metadata $ title <- fs :: path_file(fs :: path_ext_remove(script ))
94+ }
95+
96+ preamble_text <- paste(
4897 " #'" ,
49- xfun :: split_lines(as_yaml_block(list ( title = filename ) ))
98+ xfun :: split_lines(as_yaml_block(metadata ))
5099 )
51- new_content <- c(preamble , " " , content ) # Changed "\n" to ""
100+
101+ new_content <- c(preamble_text , " " , content )
52102 xfun :: write_utf8(new_content , con = script )
53103
54104 cli :: cli_inform(" Added spin preamble to {.file {script}}" )
0 commit comments