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
Changes from 2 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)))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to check really if using knit_print() at this level works really. I don't recall what knitr expect as output , but also it seems to me that there is a special mechanism in reticulate with .engine_context$pending_plots to store some content produce and then make it work with options in eng_python(). Next if (isHtml && py_has_method(value, "_repr_html_")) does something to output HTML raw data. I wonder if this is needed or not.

I don't know if you tested by at least, it is missing an argument here

Suggested change
if (knitr::opts_knit$get("rmarkdown.df_print") != "default" && renderDF) {
return(knitr::knit_print(py_to_r(value)))
}
if (knitr::opts_knit$get("rmarkdown.df_print") != "default" && renderDF) {
return(knitr::knit_print(py_to_r(value), options))
}

After this is fix it is working as expected it seems. (object is passed in captured and added to outputs$data() which is correctly passed to engine_output(). So looks good to me.

Copy link
Member Author

@dfalbel dfalbel Sep 12, 2023

Choose a reason for hiding this comment

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

Thanks for the fix, I made the change in 8243db4

Simply returning the raw output seems to work correctly. knit_print output seems to have a knit_html class that is correctly handled by knitr later. DO you have ideas of what could the potential edge cases? I have tried things like printing multiple tables from the same chunk and it worked.


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)

}

}
Loading