Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not add outputs when results = 'hide' is set #1600

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MyClass:
def _repr_html_(self):
return "<p>uh-oh</p>"

MyClass()
16 changes: 16 additions & 0 deletions tests/testthat/resources/knitr-results-hide.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: "Hide and include"
output: md_document
---

```{python, results = 'hide'}
class MyClass:
def _repr_html_(self):
return "<p>uh-oh</p>"

MyClass()
```

```{python, include = FALSE}
1 + 1
```
17 changes: 17 additions & 0 deletions tests/testthat/test-python-knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading