rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.67k stars 327 forks source link

support plotly plots in R Markdown documents #711

Closed kevinushey closed 2 years ago

kevinushey commented 4 years ago

E.g.

```{python}
import plotly.express as px
fig = px.scatter(x=[1, 2, 3], y=[5, 4, 6])
fig
gvwilson commented 4 years ago

We would really like this (and Altair support) for teaching purposes.

nchelaru commented 4 years ago

+1 for me! This would be fantastic!

vermosen commented 4 years ago

+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")
XiangyunHuang commented 4 years ago

+1 Will be awesome

ttimbers commented 3 years ago

I would love this as well!

nischalshrestha commented 3 years ago

+1 for teaching purposes as well!

joelostblom commented 3 years ago

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
ttimbers commented 3 years ago

I should clarify, that my upvote was primarily for altair.

cderv commented 3 years ago

@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

See example here ````md --- title: "Altair-reticulate" output: html_document --- ```{r load_packages, message=FALSE} 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 # You must install `py_install(c("altair", "pandas"))` ``` ```{python} 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 ``` ```{r} res <- as_vegaspec(py$chart1) vegawidget:::knit_print.vegaspec(res) ``` ```` ![image](https://user-images.githubusercontent.com/6791940/101139803-5a772100-3612-11eb-8d41-30a918504881.png)
kevinushey commented 3 years ago

Dumping some of the conversation with @jjallaire:

jjallaire commented 3 years ago

@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.

kevinushey commented 2 years ago

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.

antonio-yu commented 2 years ago

@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.