Skip to content

Commit

Permalink
fix: knitr 1.44 doesn't support opts_current$set()
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Sep 11, 2023
1 parent 388ada2 commit b679857
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
12 changes: 8 additions & 4 deletions R/utils-knitr.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
knitr_current_label <- function() {
if (isTRUE(knitr::opts_current$get("...inline_chunk"))) {
if (isTRUE(.globals$inline_chunk)) {
return("___inline_chunk___")
}

Expand Down Expand Up @@ -37,13 +37,17 @@ knitr_chunk_specific_options <- function(label = knitr_current_label()) {
# previous chunk -- or at least `opts_current` returns the previous chunk's
# options. This inline chunk detector could probably be built into knitr in some
# way: https://github.com/yihui/knitr/issues/1988
# Prior to knitr 1.44 we could use `opts_current$set()` to set an inline chunk
# option, but modifying the current chunk options will now throw an error,
# see: https://github.com/yihui/knitr/issues/1798
# nocov start
knitr_register_detect_inline <- function() {
if ("...detect_inline_chunk" %in% knitr::opts_chunk$get()) {
# We've already registered the global option we hook into
return()
}

# We key off this chunk to always set inline chunk status
# We key off this chunk options to always set inline chunk status
knitr::opts_chunk$set(...detect_inline_chunks = TRUE)

# Set `...inline_chunk` chunk option to FALSE when entering any
Expand All @@ -56,10 +60,10 @@ knitr_register_detect_inline <- function() {

knitr_hook_detect_inline_chunk <- function(before, ...) {
# Set to FALSE inside a code chunk, reset to TRUE outside
knitr::opts_current$set(...inline_chunk = !before)
.globals$inline_chunk <- !before
}

knitr_is_inline_chunk <- function() {
knitr::opts_current$get("...inline_chunk") %||%
.globals$inline_chunk %||%
is.null(knitr::opts_current$get("label"))
}
9 changes: 1 addition & 8 deletions tests/testthat/rmds/use-chunk_chunk-opts.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ the_data <- list(
list(first = "nine", second = "ten")
)
knitr::opts_chunk$set(.data = the_data[[1]], ...detect_inline_chunks = TRUE)
knitr::knit_hooks$set(...detect_inline_chunks = function(before, options) {
if (before) {
knitr::opts_current$set(...inline_chunk = FALSE)
} else {
knitr::opts_current$set(...inline_chunk = TRUE)
}
})
knitr::opts_chunk$set(.data = the_data[[1]])
```

```{epoxy chunk-template, .data = the_data[[2]]}
Expand Down

0 comments on commit b679857

Please sign in to comment.