I am attempting to create a drill-through ggplot/ggiraph plot inside of modules, and would like to use a modal to display the resulting plot.
I have a basic example that almost works, but only for the first time I select a value on the plot. After that, the modal does not display a plot. I say it “almost” works because the plot does display in the modal, but it displays without the interactivity that it should have using ggiraph.
Any thoughts on how to display the plot in response to subsequent selections would be greatly appreciated.
library(shiny)
library(tidyverse)
library(ggiraph)
# Modal module UI
modalModuleUI <- function(id) {
ns <- NS(id)
tagList(
ggiraphOutput(ns("mainplot"))
)
}
plotModal <- function(session) {
ns <- session$ns
modalDialog(
p("Cylinder Plot"),
ggiraphOutput(ns("modalplot"))
)
}
# Modal module server
modalModule <- function(input, output, session) {
output$mainplot <-renderggiraph({
p <- ggplot(mpg, aes( x = class, tooltip = class,
data_id = class ) ) +
geom_bar_interactive()
ggiraph(code = print(p), selection_type = "single")
})
output$modalplot <- renderggiraph({
selected_class <- input$mainplot_selected
# selected_class <- compact
df <- mpg %>%
filter(class == selected_class)
p <- ggplot(df, aes(x = cyl, tooltip = class))+
geom_bar_interactive()
ggiraph(code = print(p), selection_type = "single")
})
# open modal on plot click
observeEvent(input$mainplot_selected,
ignoreNULL = TRUE,
ignoreInit = TRUE,
showModal(plotModal(session))
)
}
# Main app UI
ui <- fluidPage(modalModuleUI("foo"))
# Main app server
server <- function(input, output, session) {
callModule(modalModule, "foo")
}
shinyApp(ui, server)
```r