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

Improves data frame handling #1474

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 25 additions & 3 deletions R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,25 @@ eng_python_autoprint <- function(captured, options) {

} else if (inherits(value, "pandas.core.frame.DataFrame")) {

return(captured)
# this comes from https://github.com/yihui/knitr/blob/7bc8b393e261c88f299bccc7eee40b4e952ebd57/R/output.R#L197
isQuarto <- !is.null(knitr::opts_knit$get('quarto.version'))
renderDF <- getOption("reticulate.engine.render_df", default = TRUE)

# Quarto documents running Python with the Jupyter engine return richly rendered
# data.frames and we want keep the same behavior for documents rendered with
# the knitr engine
if (isQuarto && renderDF) {
return(eng_python_generic_autoprint(captured, value))
}

# we respect the Rmarkdown `df_print` option that allows to control how
# to display data.frames in the document. In the case it's not the default,
# we cast into an R data.frame and let knitr handle the rendering.
if (knitr::opts_knit$get("rmarkdown.df_print") != "default" && renderDF) {
return(knitr::knit_print(py_to_r(value), options = options))
}

return(captured)
} else if (isHtml && py_has_method(value, "_repr_html_")) {

py_capture_output({
Expand Down Expand Up @@ -730,7 +747,13 @@ eng_python_autoprint <- function(captured, options) {

return("")

} else if (py_has_method(value, "_repr_markdown_")) {
} else {
return(eng_python_generic_autoprint(captured, value))
}
}

eng_python_generic_autoprint <- function(captured, value) {
if (py_has_method(value, "_repr_markdown_")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As commented in main comment, I wonder if we should do as Quarto and

  • Output HTML first if available
  • Output Markdown if available
  • Then captured (equivalent to text/plain)

Logic in Quarto for display is at displayMimeType() where we have a logic of ordering possibly output before checking the output values. In this logic, if there is HTML and Markdown representation, HTML is preferred currently


data <- as_r_value(value$`_repr_markdown_`())
.engine_context$pending_plots$push(knitr::asis_output(data))
Expand All @@ -748,5 +771,4 @@ eng_python_autoprint <- function(captured, options) {
return(captured)

}

}
439 changes: 439 additions & 0 deletions tests/testthat/_snaps/python-knitr-engine/default.html

Large diffs are not rendered by default.

888 changes: 888 additions & 0 deletions tests/testthat/_snaps/python-knitr-engine/kable.html

Large diffs are not rendered by default.

1,680 changes: 1,680 additions & 0 deletions tests/testthat/_snaps/python-knitr-engine/paged.html

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions tests/testthat/resources/pandas-autoprint.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: "Python Multiple Print & Comments"
---

```{python}
r.mtcars
```

22 changes: 22 additions & 0 deletions tests/testthat/test-python-knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ test_that("Output streams are remaped when kniting", {

expect_snapshot_file(test_path("resources", "knitr-print2.md"))
})

test_that("correctly handles `df_print` for pandas data frames", {

skip_on_cran()
skip_if_not_installed("rmarkdown")
local_edition(3)

owd <- setwd(test_path("resources"))
on.exit({setwd(owd)}, add = TRUE)
for (df_print in c("paged", "default", "kable")) {
out_file <- paste0(df_print, ".html")
rmarkdown::render(
"pandas-autoprint.Rmd",
quiet = TRUE,
output_file = out_file,
output_format = rmarkdown::html_document(
df_print = df_print
))
expect_snapshot_file(out_file)
}

})