Closed kevinushey closed 2 years ago
We would really like this (and Altair support) for teaching purposes.
+1 for me! This would be fantastic!
+1 for the feature !
As a workaround, you can have in a python chunk
fig = ...
plotly.io.write_html(fig, file='my_plot.html', auto_open=False)
and then a one liner in R:
htmltools::includeHTML("my_plot.html")
+1 Will be awesome
I would love this as well!
+1 for teaching purposes as well!
I would love to see Altair support, it seems like the foundation for this already exists in vegawidget, for example something like this works in a notebook inside RStudio, but not when knitting:
---
title: "Altair-reticulate"
output: html_document
---
```{r load_packages}
library(vegawidget) # Need to install this using: install.packages('vegawidget')
library(reticulate)
# use_python('/usr/local/bin/python3') # you may not need this, comment out if so
import altair as alt
import pandas as pd
source = pd.DataFrame({
'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})
chart1 = alt.Chart(source).mark_bar().encode(
alt.X('a:N',title='X-Axis label'),
alt.Y('b:Q',title='Y-axis label')).to_json()
chart1
as_vegaspec(py$chart1)
Related issues:
- https://github.com/rstudio/rstudio/issues/4259
- https://github.com/altair-viz/altair/issues/1940
I should clarify, that my upvote was primarily for altair.
@joelostblom that is a nice way of interleaving R and Python ! All the more because it should work while knitting because vegawidget:::knit_print.vegaspec()
exists. Also it was working previously from what I could see (https://github.com/altair-viz/altair/issues/1940#issuecomment-581218119 and https://github.com/vegawidget/vegawidget/issues/112).
It seems there is something wrong with knitr::knit_print
dispatch to the corresponding method vegawidget:::knit_print.vegaspec()
. It you call the function directly, it will work but obviously you shouldn't call this function directly, so issue with knitr or vegawidget, I don't know yet.
I see you opened one https://github.com/vegawidget/vegawidget/issues/132) - I'll comment there too
Dumping some of the conversation with @jjallaire:
Some Python objects provide a _repr_html_
method, which we could use for Notebook outputs.
That HTML might implicitly depend on some external scripts; for example require.js and jQuery. We may also need to make jQuery available to the require universe via define('jquery', [],function() {return window.jQuery;})
.
We might be able to use knitr::asis_output(...)
with the generated HTML, plus any relevant JavaScript dependencies passed as part of the meta
argument. We may also be able to use htmltools::htmlDependencies()
to accomplish something similar.
leaflet
might require some more hand-holding, based on the different types of output (state and data as JSON) and the ordering of those in the generated document may also need special handling.
@kevinushey Note that for plotly I think it would be fine to load the library from the CDN (as the default HTML representation does) as opposed to embedding as is done in Jupyter. I generally prefer the embedded approach for offline / longer-term reproducibility however every other Jupyter widget I have seen uses a CDN so this is more or less expected behavior.
It looks like the associated reticulate
support has been implemented; these plots are now properly shown when an R Markdown document using plotly
is rendered. https://github.com/rstudio/rstudio/issues/8762 tracks the work required for this to happen in the IDE for chunk execution as well.
@kevinushey
The plotly plots can't be rendered inline in a python chunk yet.
import plotly.express as px
fig = px.scatter(x=[1, 2, 3], y=[5, 4, 6])
fig
The code above only shows the origin data of fig.
E.g.