|
23 | 23 | #' - Adding the document's YAML metadata as a spin-style header |
24 | 24 | #' - Creating an R script that can be rendered with the same options |
25 | 25 | #' |
| 26 | +#' ## Chunk option handling: |
| 27 | +#' - Chunks with `purl: false` are completely skipped and not included in the output |
| 28 | +#' - Chunks with `eval: false` have their code commented out (prefixed with `# `) in the R script |
| 29 | +#' - All other chunk options are preserved as `#|` comment headers |
| 30 | +#' |
26 | 31 | #' ## File handling: |
27 | 32 | #' - If the output R script already exists, the function will abort with an error |
28 | 33 | #' - Non-R code cells (e.g., Python, Julia, Observable JS) are ignored |
|
35 | 40 | #' more details on rendering R scripts with Quarto. |
36 | 41 | #' |
37 | 42 | #' The resulting R script uses Quarto's executable cell format with `#|` |
38 | | -#' comments to preserve chunk options like `echo`, `eval`, `output`, etc. |
| 43 | +#' comments to preserve chunk options like `label`, `echo`, `output`, etc. |
| 44 | +#' |
| 45 | +#' The resulting R script could also be `source()`d in R, as any `eval = FALSE` will be commented out. |
| 46 | +#' |
| 47 | +#' ## Limitations: |
| 48 | +#' This function relies on static analysis of the Quarto document by `quarto inspect`. This means that |
| 49 | +#' any \pkg{knitr} specific options like `child=` or specific feature like [knitr::read_chunk()] are not supported. |
| 50 | +#' They rely on tangling or knitting by \pkg{knitr} itself. For this support, |
| 51 | +#' one should look at [knitr::hook_purl()] or [knitr::purl()]. |
39 | 52 | #' |
40 | 53 | #' @return Invisibly returns the path to the created R script file, or |
41 | 54 | #' `NULL` if no R code cells were found. |
@@ -110,12 +123,22 @@ qmd_to_r_script <- function(qmd, script = NULL) { |
110 | 123 | } |
111 | 124 |
|
112 | 125 | r_codeCells <- codeCells[codeCells$language == "r", ] |
113 | | - |
114 | 126 | content <- character(nrow(r_codeCells)) |
115 | 127 | for (i in seq_len(nrow(r_codeCells))) { |
116 | 128 | row <- r_codeCells[i, ] |
117 | 129 | metadata_list <- as.list(row$metadata) |
118 | 130 | metadata_clean <- metadata_list[!is.na(metadata_list)] |
| 131 | + if (isFALSE(metadata_clean$purl)) { |
| 132 | + # cell with purl: false should be skipped |
| 133 | + next |
| 134 | + } |
| 135 | + if (isFALSE(metadata_clean$eval)) { |
| 136 | + # cell with eval: false should be commented out in R script |
| 137 | + row$source <- paste( |
| 138 | + c(paste0("# ", head(xfun::split_lines(row$source), -1)), ""), |
| 139 | + collapse = "\n" |
| 140 | + ) |
| 141 | + } |
119 | 142 | content[i] <- paste( |
120 | 143 | c(create_code_preamble(metadata_clean), row$source), |
121 | 144 | collapse = "\n" |
|
0 commit comments