diff --git a/R/knitr-engine.R b/R/knitr-engine.R index 0876b59fe..8c2983e6e 100644 --- a/R/knitr-engine.R +++ b/R/knitr-engine.R @@ -197,7 +197,7 @@ eng_python <- function(options) { # Stash some options. is_hold <- identical(options$results, "hold") - is_include <- isTRUE(options$include) + is_hidden <- identical(options$results, "hide") jupyter_compat <- isTRUE(options$jupyter_compat) # line index from which source should be emitted @@ -305,8 +305,8 @@ eng_python <- function(options) { outputs$push(output) } - # append captured outputs (respecting 'include' option) - if (is_include) { + # append captured outputs (respecting 'results = "hide"' option) + if (!is_hidden) { # append captured output if (!identical(captured, "")) outputs_target$push(captured) @@ -348,10 +348,11 @@ eng_python <- function(options) { plt$show() } - for (plot in .engine_context$pending_plots$data()) - outputs_target$push(plot) - .engine_context$pending_plots$clear() - + if (!is_hidden) { + for (plot in .engine_context$pending_plots$data()) + outputs_target$push(plot) + .engine_context$pending_plots$clear() + } # if we were using held outputs, we just inject the source in now if (is_hold) { diff --git a/tests/testthat/_snaps/python-knitr-engine/knitr-results-hide.md b/tests/testthat/_snaps/python-knitr-engine/knitr-results-hide.md new file mode 100644 index 000000000..9f78a3d57 --- /dev/null +++ b/tests/testthat/_snaps/python-knitr-engine/knitr-results-hide.md @@ -0,0 +1,5 @@ + class MyClass: + def _repr_html_(self): + return "

uh-oh

" + + MyClass() diff --git a/tests/testthat/resources/knitr-results-hide.Rmd b/tests/testthat/resources/knitr-results-hide.Rmd new file mode 100644 index 000000000..050e40e98 --- /dev/null +++ b/tests/testthat/resources/knitr-results-hide.Rmd @@ -0,0 +1,16 @@ +--- +title: "Hide and include" +output: md_document +--- + +```{python, results = 'hide'} +class MyClass: + def _repr_html_(self): + return "

uh-oh

" + +MyClass() +``` + +```{python, include = FALSE} +1 + 1 +``` diff --git a/tests/testthat/test-python-knitr-engine.R b/tests/testthat/test-python-knitr-engine.R index bd7300a1a..d972b4f2e 100644 --- a/tests/testthat/test-python-knitr-engine.R +++ b/tests/testthat/test-python-knitr-engine.R @@ -86,6 +86,23 @@ test_that("knitr 'warning=FALSE' option", { }) +test_that("knitr results='hide' and include = FALSE options", { + + skip_on_cran() + skip_if_not_installed("rmarkdown") + + local_edition(3) # needed for expect_snapshot_file() + + owd <- setwd(test_path("resources")) + rmarkdown::render("knitr-results-hide.Rmd", quiet = TRUE) + setwd(owd) + + rendered <- test_path("resources", "knitr-results-hide.md") + + expect_snapshot_file(rendered) + +}) + test_that("Output streams are remaped when kniting", { skip_on_cran()