From 7d0745de1b2ccaa374527aac6f3dbc6d77ba3a2a Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 23 Apr 2024 16:01:30 -0400 Subject: [PATCH 1/4] Do not add outputs when `results = 'hide'` is set --- R/knitr-engine.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/knitr-engine.R b/R/knitr-engine.R index 0876b59fe..229cb2204 100644 --- a/R/knitr-engine.R +++ b/R/knitr-engine.R @@ -197,6 +197,7 @@ eng_python <- function(options) { # Stash some options. is_hold <- identical(options$results, "hold") + is_hidden <- identical(options$results, "hide") is_include <- isTRUE(options$include) jupyter_compat <- isTRUE(options$jupyter_compat) @@ -306,7 +307,7 @@ eng_python <- function(options) { } # append captured outputs (respecting 'include' option) - if (is_include) { + if (!is_hidden && is_include) { # append captured output if (!identical(captured, "")) outputs_target$push(captured) From 8041ace96c128ef2bff06a4bc869135d08477756 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 23 Apr 2024 16:04:14 -0400 Subject: [PATCH 2/4] Also hide for matplotlib special handling --- R/knitr-engine.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/R/knitr-engine.R b/R/knitr-engine.R index 229cb2204..c7fd3c620 100644 --- a/R/knitr-engine.R +++ b/R/knitr-engine.R @@ -349,10 +349,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 && is_include) { + 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) { From 5384c9165afc6d969022ef377e5a20c33eebb875 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 23 Apr 2024 16:06:23 -0400 Subject: [PATCH 3/4] include is handled directly by knitr for all engine --- R/knitr-engine.R | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/R/knitr-engine.R b/R/knitr-engine.R index c7fd3c620..8c2983e6e 100644 --- a/R/knitr-engine.R +++ b/R/knitr-engine.R @@ -198,7 +198,6 @@ eng_python <- function(options) { # Stash some options. is_hold <- identical(options$results, "hold") is_hidden <- identical(options$results, "hide") - is_include <- isTRUE(options$include) jupyter_compat <- isTRUE(options$jupyter_compat) # line index from which source should be emitted @@ -306,8 +305,8 @@ eng_python <- function(options) { outputs$push(output) } - # append captured outputs (respecting 'include' option) - if (!is_hidden && is_include) { + # append captured outputs (respecting 'results = "hide"' option) + if (!is_hidden) { # append captured output if (!identical(captured, "")) outputs_target$push(captured) @@ -349,7 +348,7 @@ eng_python <- function(options) { plt$show() } - if (!is_hidden && is_include) { + if (!is_hidden) { for (plot in .engine_context$pending_plots$data()) outputs_target$push(plot) .engine_context$pending_plots$clear() From 5e01a472e1b014a98880b0db1306ecdb2079e37e Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 24 Apr 2024 10:24:25 -0400 Subject: [PATCH 4/4] Add a test for results and include --- .../python-knitr-engine/knitr-results-hide.md | 5 +++++ tests/testthat/resources/knitr-results-hide.Rmd | 16 ++++++++++++++++ tests/testthat/test-python-knitr-engine.R | 17 +++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 tests/testthat/_snaps/python-knitr-engine/knitr-results-hide.md create mode 100644 tests/testthat/resources/knitr-results-hide.Rmd 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()