I'm using a quarto dashboard with some shiny for the interactivity functionality.
At the moment I have a side bar with some filters that loads the data and after the data is loaded a scatter plot (via plotly) is created. Using the plotly_selected event data I'm able to filter other datatables and plots in the dashboard.
The issue is that once I go back to the side bar, change the filters and load the data, if it's a subset of the first data - which was selected before - the selection doesn't reset.
An example, if in the first data I have 20 data points and select 3, after I use the filters and 2 data points coincide with the ones before, now I have 2 selected instead of just showing all data points from that subset.
I tried solving this using this solution: Reseting click-events - #2 by helpmeplot - Plotly R - Plotly Community Forum
But it seems that the useShinyjs()
only works inside a fluidPage()
which I actually don't use since in quarto dashboards we can just use the R cards for the UI part (as shown here: Quarto – Dashboards with Shiny for R)
My question is: Is there a way to make this useShinyjs() work in a quarto dashboard or is there some other way to reset the event data that might work for the dashboard?
This is an example of my code at the moment:
---
title: "Test"
format: dashboard
server: shiny
---
```{r}
#| context: setup
library(ggplot2)
library(shinyjs)
dataset <- diamonds
{.sidebar}
numericRangeInput(
"carat_filter",
"carat",
c(0, 1),
separator = "to",
min = 0,
max = 1)
div(style="float: right", actionButton("sidebar_ok", "OK"))
Plot
useShinyjs()
extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('plotly_selected-diamonds_plot', 'null'); }")
plotlyOutput('plot')
tableOutput('data')
#| context: server
dataset_filtered <- reactiveVal(NULL)
observeEvent(input$sidebar_ok, {
data <- dataset %>%
filter(carat >= carat_filter[1] & carat<= carat_filter[2])
if (!is.null(filtered_df)) {
dataset_filtered(data)
js$resetClick()
}
})
output$plot <- renderPlotly({
p <- ggplot(
dataset_filtered(),
aes_string(x=input$x, y=input$y)) + geom_point()
return(ggplotly(p, source = "diamonds_plot"))
})
observe({
selected_data <- event_data("plotly_selected", source = "diamonds_plot")
datapoints_selected <- dataset_filtered()
if (!is.null(selected_data)) {
data <-
dataset_filtered()[dataset_filtered()[, input$x_variable] %in% selected_data$x &
dataset_filtered()[, input$y_variable] %in% selected_data$y,]
datapoints_selected <- if(nrow(data) > 0) data else dataset_filtered()
}
output$data <- renderTable({
datapoints_selected
})
})