-
Notifications
You must be signed in to change notification settings - Fork 327
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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))) | ||
} | ||
|
||
return(captured) | ||
} else if (isHtml && py_has_method(value, "_repr_html_")) { | ||
|
||
py_capture_output({ | ||
|
@@ -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_")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Logic in Quarto for display is at |
||
|
||
data <- as_r_value(value$`_repr_markdown_`()) | ||
.engine_context$pending_plots$push(knitr::asis_output(data)) | ||
|
@@ -748,5 +771,4 @@ eng_python_autoprint <- function(captured, options) { | |
return(captured) | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
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 ineng_python()
. Nextif (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
After this is fix it is working as expected it seems. (object is passed in
captured
and added tooutputs$data()
which is correctly passed toengine_output()
. So looks good to me.There was a problem hiding this comment.
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 aknit_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.