I want to create an interactive bar chart that lets users filter observations based on a range of values, and then renders counts per class for the selected time period dynamically. Since the filtered data needs to be available for numerous such graphs, I thought a combination of crosstalk and plotly/ggplot might prove valuable.
I attached a reprex further below that uses shared data and filtering functionality from crosstalk to allow for the dynamic filtering part. When I knit the document, the bar chart renders nicely as long as the full range of values is selected (default).
However, the plotting region becomes empty for any other, ie. user-adjusted range.
What exactly am I missing here? I assume there must be a difference between full and filtered shared datasets that ggplotly()
cannot handle proberly. Is there maybe another approach that I could follow to achieve my goal?
Here's the content of my .Rmd file:
---
title: mpg class counts filtered by time period
output: html_document
---
```{r echo = FALSE, message = FALSE, warning = FALSE}
library(crosstalk)
library(plotly)
# Wrap data frame in SharedData
sd = SharedData$new(mpg)
# Create a filter input
filter_slider("Year", "Year", sd, column = ~ year, step = 1, width = 250)
# Render graph
bscols(
ggplotly(
ggplot(aes(x = class), data = sd) +
geom_bar()
)
)
```