Skip to content

Commit

Permalink
Throw error when deferred_run() is run in knitted document
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Jan 12, 2024
1 parent 90df123 commit 8c32672
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/defer.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ defer_parent <- function(expr, priority = c("first", "last")) {
#' @rdname defer
#' @export
deferred_run <- function(envir = parent.frame()) {
if (knitr_in_progress()) {
stop("Can't run `deferred_run()` in a knitted document")
}
if (is_top_level_global_env(envir)) {
handlers <- the$global_exits
} else {
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-defer.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,18 @@ test_that("`frame_exits()` and `frame_clear_exits()`", {
frame_clear_exits()
expect_equal(frame_exits(), list())
})

test_that("can't run `deferred_run()` in knitr", {
skip_if_cannot_knit()

rmd <- local_tempfile(fileext = ".Rmd")
writeLines(rmd, text = "
```{r}
withr::deferred_run()
```
")
expect_error(
suppressMessages(rmarkdown::render(rmd, quiet = TRUE)),
"in a knitted document"
)
})

0 comments on commit 8c32672

Please sign in to comment.