Skip to content

Commit 5aa318a

Browse files
authored
Override default knitr chunk options with roxy_meta (#1390)
Fixes #1240
1 parent b5bfe31 commit 5aa318a

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# roxygen2 (development version)
22

3+
* The new `knitr_chunk_options` option (in the `Roxygen` entry of
4+
`DESCRIPTION` or in `man/roxygen/meta.R`) is added to the knitr chunk
5+
options that roxygen2 uses for markdown code blocks and inline
6+
code (#1390).
7+
38
* `@includeRmd` now gives better feedback when it fails (#1089).
49

510
* `@export` will now export both the class and constructor function when

R/markdown.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ eval_code_node <- function(node, env) {
154154
# write knitr markup for fenced code
155155
text <- paste0("```", if (!is.na(lang)) lang, "\n", xml_text(node), "```\n")
156156
}
157-
roxy_knit(text, env, knitr_chunk_defaults)
157+
158+
chunk_opts <- utils::modifyList(
159+
knitr_chunk_defaults,
160+
as.list(roxy_meta_get("knitr_chunk_options", NULL))
161+
)
162+
163+
roxy_knit(text, env, chunk_opts)
158164
}
159165

160166
knitr_chunk_defaults <- list(

R/options.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#' * `rd_family_title` `<list>`: overrides for `@family` titles. See the
2929
#' _rd_ vignette for details: `vignette("rd", package = "roxygen2")`
3030
#'
31+
#' * `knitr_chunk_options`: default chunk options used for knitr.
32+
#'
3133
#' @section How to set:
3234
#' Either set in `DESCRIPTION`:
3335
#'
@@ -60,7 +62,8 @@ load_options <- function(base_path = ".") {
6062
markdown = FALSE,
6163
r6 = TRUE,
6264
current_package = NA_character_,
63-
rd_family_title = list()
65+
rd_family_title = list(),
66+
knitr_chunk_options = NULL
6467
)
6568

6669
unknown_opts <- setdiff(names(opts), names(defaults))
@@ -136,3 +139,10 @@ roxy_meta_load <- function(base_path = getwd()) {
136139
env_bind(roxy_meta, !!!load_options(base_path))
137140
}
138141

142+
local_roxy_meta_set <- function(key, value, envir = caller_env()) {
143+
old_value <- roxy_meta_get(key)
144+
withr::defer(roxy_meta_set(key, old_value), envir = envir)
145+
146+
roxy_meta_set(key, value)
147+
}
148+

man/load_options.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-markdown.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,18 @@ test_that("alternative knitr engines", {
633633
"))
634634
)
635635
})
636+
637+
test_that("can override default options", {
638+
local_roxy_meta_set("knitr_chunk_options", list(comment = "###"))
639+
640+
out <- roc_proc_text(rd_roclet(), "
641+
#' Title
642+
#'
643+
#' ```{r}
644+
#' 1+1
645+
#' ```
646+
#' @md
647+
foo <- function() { }
648+
")[[1]]
649+
expect_match(out$get_section("description")$value, "###", fixed = TRUE)
650+
})

vignettes/rd-formatting.Rmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ Code blocks support some knitr chunk options, e.g. to keep the output of several
317317
Some knitr chunk options are reset at the start of every code block, so if you want to change these, you'll have to specify them for every chunk.
318318
These are currently `r paste0("\x60", names(roxygen2:::knitr_chunk_defaults), "\x60")`.
319319

320+
Alternatively, you can set the `knitr_chunk_options` option to override these defaults, or add new chunk options that are used for the whole package.
321+
See `?load_options` for specifying roxygen2 options.
322+
320323
### Images
321324

322325
Plots will create `.png` files in the `man/figures` directory with file names coming from the chunk name.

0 commit comments

Comments
 (0)