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

Add support for multiple Altair compound charts #1532

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# reticulate (development version)

- Fixed an issue where the knitr engine would not correctly display altair
compound charts if more than one were present in a document (#1500, #1532).

- Subclassed Python list and dict objects are no longer automatically converted
to R vectors. Additionally, the S3 R `class` attribute for Python objects is
now constructed using the Python `type(object)` directly, rather than from the
Expand Down
10 changes: 7 additions & 3 deletions R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ eng_python_is_altair_chart <- function(value) {
# support different API versions, assuming that the class name
# otherwise remains compatible
classes <- class(value)
pattern <- "^altair\\.vegalite\\.v[[:digit:]]+\\.api\\.Chart$"
pattern <- "^altair\\.vegalite\\.v[[:digit:]]+\\.api\\.(HConcat|VConcat|Layer|Repeat|Facet)?Chart$"
any(grepl(pattern, classes))

}
Expand Down Expand Up @@ -734,14 +734,18 @@ eng_python_autoprint <- function(captured, options) {
} else if (eng_python_is_altair_chart(value)) {

# set width if it's not already set
width <- value$width
# This only applies to Chart objects, compound charts like HConcatChart
# don't have a 'width' or 'height' property attribute.
# TODO: add support for propagating width/height options from knitr to
# altair compound charts
width <- py_get_attr(value, "width", TRUE)
if (inherits(width, "altair.utils.schemapi.UndefinedType")) {
width <- options$altair.fig.width %||% options$out.width.px %||% 810L
value <- value$properties(width = width)
}

# set height if it's not already set
height <- value$height
height <- py_get_attr(value, "height", TRUE)
if (inherits(height, "altair.utils.schemapi.UndefinedType")) {
height <- options$altair.fig.height %||% options$out.height.px %||% 400L
value <- value$properties(height = height)
Expand Down
49 changes: 46 additions & 3 deletions tests/testthat/resources/altair-example.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ output:
```{r}
library(reticulate)

python <- "~/.virtualenvs/python-3.7.7-venv/bin/python"
if (file.exists(python))
use_python(python, required = TRUE)
# py_install(c("altair", "vega_datasets", "pandas"))
```

```{python, altair.fig.width=800, altair.fig.height=300}
Expand All @@ -25,3 +23,48 @@ alt.Chart(source).mark_bar().encode(
y='count()',
)
```

## Compound charts

```{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]
})

fig1 = alt.Chart(source).mark_bar().encode(
x='a',
y='b'
)
fig2 = alt.Chart(source).mark_bar().encode(
x='a',
y='b',
color='b:N'
)
fig3 = alt.Chart(source).mark_bar().encode(
x='a',
y='b',
color='a:N'
)
```

## Compound Chart1
```{python}
#| label: first
(fig1 | fig2).properties(title="This is the First Chart")
```

## Compound Chart2
```{python}
#| label: second
(fig2 | fig1).properties(title="This is the Second Chart")
```

## Compound Chart3
```{python}
#| label: third
(fig2 | fig1 | fig3).properties(title="This is the Third Chart")
```
Loading