From b401fcbcb6ab7f13b05c25c4a6427ce8ecd1e62f Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Tue, 25 Jul 2023 08:38:52 -0300 Subject: [PATCH] Still include the stdout output captured before the autoprint mechanism. --- R/knitr-engine.R | 6 +-- .../_snaps/python-knitr-engine/knitr-print.md | 39 +++++++++++++++++++ .../python-knitr-engine/knitr-print2.md | 33 ++++++++++++++++ tests/testthat/resources/knitr-print.Rmd | 25 ++++++++++++ 4 files changed, 100 insertions(+), 3 deletions(-) diff --git a/R/knitr-engine.R b/R/knitr-engine.R index 5251d8c60..5928649d3 100644 --- a/R/knitr-engine.R +++ b/R/knitr-engine.R @@ -197,21 +197,21 @@ eng_python <- function(options) { py_compile_eval("'__reticulate_placeholder__'") # run code and capture output - captured <- if (capture_errors) + captured_stdout <- if (capture_errors) tryCatch(py_compile_eval(snippet, 'single'), error = identity) else py_compile_eval(snippet, 'single') # handle matplotlib plots and other special output captured <- eng_python_autoprint( - captured = captured, + captured = captured_stdout, options = options ) # A trailing ';' suppresses output. # In jupyter mode, only the last expression in a chunk has repr() output. if (grepl(";\\s*$", snippet) | (jupyter_compat & !last_range)) - captured <- "" + captured <- captured_stdout # emit outputs if we have any has_outputs <- diff --git a/tests/testthat/_snaps/python-knitr-engine/knitr-print.md b/tests/testthat/_snaps/python-knitr-engine/knitr-print.md index 330e35834..93bbbfc6a 100644 --- a/tests/testthat/_snaps/python-knitr-engine/knitr-print.md +++ b/tests/testthat/_snaps/python-knitr-engine/knitr-print.md @@ -2,3 +2,42 @@ bt$print("Hello world") ## Hello world + +Both in `jupyter_compat = TRUE` and `jupyter_compat = FALSE` we should +see the results, because a `print` was called: + + print(1) + + ## 1 + + print(2) + + ## 2 + + print(1) + + ## 1 + + print(2) + + ## 2 + +For the `jupyter_compat = FALSE` mode we should see the output of both +expressions. In `jupyter_compat`, we should only see the output for the +last expression. + + 1 + 0 + + ## 1 + + 1 + 1 + + ## 2 + + 1 + 0 + + ## 1 + + 1 + 1 + + ## 2 diff --git a/tests/testthat/_snaps/python-knitr-engine/knitr-print2.md b/tests/testthat/_snaps/python-knitr-engine/knitr-print2.md index 330e35834..ec0a7bf72 100644 --- a/tests/testthat/_snaps/python-knitr-engine/knitr-print2.md +++ b/tests/testthat/_snaps/python-knitr-engine/knitr-print2.md @@ -2,3 +2,36 @@ bt$print("Hello world") ## Hello world + +Both in `jupyter_compat = TRUE` and `jupyter_compat = FALSE` we should +see the results, because a `print` was called: + + print(1) + + ## 1 + + print(2) + + ## 2 + + print(1) + print(2) + + ## 2 + +For the `jupyter_compat = FALSE` mode we should see the output of both +expressions. In `jupyter_compat`, we should only see the output for the +last expression. + + 1 + 0 + + ## 1 + + 1 + 1 + + ## 2 + + 1 + 0 + 1 + 1 + + ## 2 diff --git a/tests/testthat/resources/knitr-print.Rmd b/tests/testthat/resources/knitr-print.Rmd index e0693a546..cb50f1ea9 100644 --- a/tests/testthat/resources/knitr-print.Rmd +++ b/tests/testthat/resources/knitr-print.Rmd @@ -8,3 +8,28 @@ bt <- reticulate::import_builtins() bt$print("Hello world") ``` +Both in `jupyter_compat = TRUE` and `jupyter_compat = FALSE` we should see the +results, because a `print` was called: + +```{python} +print(1) +print(2) +``` + +```{python, jupyter_compat = TRUE} +print(1) +print(2) +``` + +For the `jupyter_compat = FALSE` mode we should see the output of both expressions. +In `jupyter_compat`, we should only see the output for the last expression. + +```{python} +1 + 0 +1 + 1 +``` + +```{python, jupyter_compat = TRUE} +1 + 0 +1 + 1 +```