Hi - is it currently possible to use Altair when rendering python chunks in RMarkdown?
For example, I have the RMarkdown file:
title: "Test"
```{python}
import pandas as pd
import altair as alt
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
```{python}
df.plot()
```{python}
alt.Chart(df).mark_point()
````
and render it with:
```bash
Rscript -e "rmarkdown::render('test.Rmd')"
The 2nd code chunk display a matplotlib graphic in the output pdf, but the altair chart is just displayed as ## alt.Chart(...).
I suspect this is due to Altair "rendering" a vega-lite schema or JavaScript? However since I'm rendering to a PDF, I would like to simply embed a static image. I know Altair can do this with alt.Chart(df).mark_point().save('test.png'), but I would like to embed the resulting image into the rendered RMarkdown PDF instead of to a separate file.
Do you have any advice on if this is possible or next steps?
As a followup question, would rending the RMarkdown file to HTML instead of PDF (allowing for Altair interactivity) be possible?
Thanks!
I'm not familiar enough with rmarkdown to know how to inject the JS necessary to render charts, but you may be able to find some clues in this issue: https://github.com/altair-viz/altair/issues/1141
The altair CRAN package may also be useful: https://cran.rstudio.com/web/packages/altair/index.html
With some help from the folks at vegawidget, I was able to get Altair working in an Rmd html and pdf, below is the Rmarkdown code:
---
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
```{python Altair}
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()
```{r display1}
as_vegaspec(py$chart1)
```{python Altair4}
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]
})
chart2 = alt.Chart(source).mark_bar().encode(
alt.X('a:N',title='X-Axis label'),
alt.Y('b:Q',title='Y-axis label')
).properties(title={
"text": ["Altair now has multi-line titles!!", "Second line of title"],
"subtitle": ["And subtitles! SO COOL", "Even cooler second line wow dang, but you need to update to v4 of Altair"],
"color": "red",
"subtitleColor": "green"
}).to_json()
```{r display2}
as_vegaspec(py$chart2)

h/t to @ijlyttle from this issue
Thanks for your thoughts on this! I haven't gotten the chance to try this but will go ahead and close the issue (and will re-open if I can't get it working).
Again - thanks!
Thanks! This works for me for displaying inside RStudio, but not in knitted HTML, which might be a version issue. If you would like RStudio to support Altair/Vega plots natively in RMarkdown, please give your thumbs up on this issue https://github.com/rstudio/rstudio/issues/4259.
Most helpful comment
With some help from the folks at vegawidget, I was able to get Altair working in an Rmd html and pdf, below is the Rmarkdown code:
```{python Altair}
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()
```{python Altair4}
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]
})
chart2 = alt.Chart(source).mark_bar().encode(
alt.X('a:N',title='X-Axis label'),
alt.Y('b:Q',title='Y-axis label')
).properties(title={
"text": ["Altair now has multi-line titles!!", "Second line of title"],
"subtitle": ["And subtitles! SO COOL", "Even cooler second line wow dang, but you need to update to v4 of Altair"],
"color": "red",
"subtitleColor": "green"
}).to_json()
h/t to @ijlyttle from this issue