|
| 1 | +#' Add spin preamble to R script |
| 2 | +#' |
| 3 | +#' Adds a minimal spin preamble to an R script file if one doesn't already exist. |
| 4 | +#' The preamble includes a title derived from the filename and is formatted as |
| 5 | +#' a YAML block suitable preprended with `#'` for [knitr::spin()]. |
| 6 | +#' |
| 7 | +#' This is useful to prepare R scripts for use with |
| 8 | +#' Quarto Script rendering support. |
| 9 | +#' See <https://quarto.org/docs/computations/render-scripts.html#knitr> |
| 10 | +#' |
| 11 | +#' @section Preamble format: |
| 12 | +#' For a script named `analysis.R`, the function adds this preamble by default: |
| 13 | +#' ``` |
| 14 | +#' #' --- |
| 15 | +#' #' title: analysis |
| 16 | +#' #' --- |
| 17 | +#' #' |
| 18 | +#' |
| 19 | +#' # Original script content starts here |
| 20 | +#' ``` |
| 21 | +#' |
| 22 | +#' This is the minimal preamble required for Quarto Script rendering, so that |
| 23 | +#' [Engine Bindings](https://quarto.org/docs/computations/execution-options.html#engine-binding) works. |
| 24 | +#' |
| 25 | +#' @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. |
| 30 | +#' @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 | +#' } |
| 57 | +#' @export |
| 58 | +add_spin_preamble <- function(script, title = NULL, preamble = NULL) { |
| 59 | + if (!fs::file_exists(script)) { |
| 60 | + cli::cli_abort(c( |
| 61 | + "File {.file {script}} does not exist.", |
| 62 | + "Please provide a valid file path." |
| 63 | + )) |
| 64 | + } |
| 65 | + |
| 66 | + content <- xfun::read_utf8(script) |
| 67 | + |
| 68 | + # if files starts with a spin preamble, do nothing |
| 69 | + if (grepl("^\\s*#'", content[1])) { |
| 70 | + cli::cli_inform(c( |
| 71 | + "File {.file {script}} already has a spin preamble.", |
| 72 | + "No changes made. Edit manually if needed." |
| 73 | + )) |
| 74 | + return(invisible()) |
| 75 | + } |
| 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( |
| 97 | + "#'", |
| 98 | + xfun::split_lines(as_yaml_block(metadata)) |
| 99 | + ) |
| 100 | + |
| 101 | + new_content <- c(preamble_text, "", content) |
| 102 | + xfun::write_utf8(new_content, con = script) |
| 103 | + |
| 104 | + cli::cli_inform("Added spin preamble to {.file {script}}") |
| 105 | + return(invisible(script)) |
| 106 | +} |
0 commit comments