Hmm. Good idea.
Turns out that I get the exact same behavior in a non-modularized shiny app.
I did some additional exploring in the non-modularized version. Found that if I converted the modal's ggiraph plot to a ggplot that the plot displayed without issues.
Appears that the primary issue is with displaying ggiraph in a modal. Any thoughts on why a ggplot would display as expected, but a ggiraph plot only displays the first time around?
I've pasted both versions (with ggiraph and with ggplot below).
App with ggiraph in modal. No module
library(shiny)
library(tidyverse)
library(ggiraph)
#function for calling the modal
plotModal <- function(session) {
modalDialog(
textOutput("modalText"),
ggiraphOutput("modalplot")
)
}
# Main app UI
ui <- fluidPage( ggiraphOutput("mainplot"))
# Main app server
server <- function(input, output, session) {
#create main plot
output$mainplot <-renderggiraph({
p <- ggplot(mpg, aes( x = class, tooltip = class,
data_id = class ) ) +
geom_bar_interactive()
ggiraph(code = print(p), selection_type = "single")
})
#Create the drill-through plot
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")
})
output$modalText <- renderText({
selected_class <- input$mainplot_selected
paste("You have selected", selected_class, ".")
})
# open modal on plot click using _selected
observeEvent(input$mainplot_selected,
ignoreNULL = TRUE,
ignoreInit = TRUE,
showModal(plotModal(session))
)
}
shinyApp(ui, server)
Version 2
App with ggplot in modal. No module This one works as expected.
library(shiny)
library(tidyverse)
library(ggiraph)
#function for calling the modal
plotModal <- function(session) {
modalDialog(
textOutput("modalText"),
plotOutput("modalplot")
)
}
# Main app UI
ui <- fluidPage( ggiraphOutput("mainplot"))
# Main app server
server <- function(input, output, session) {
#create main plot
output$mainplot <-renderggiraph({
p <- ggplot(mpg, aes( x = class, tooltip = class,
data_id = class ) ) +
geom_bar_interactive()
ggiraph(code = print(p), selection_type = "single")
})
#Create the drill-through plot
output$modalplot <- renderPlot({
selected_class <- input$mainplot_selected
# selected_class <- compact
df <- mpg %>%
filter(class == selected_class)
p <- ggplot(df, aes(x = cyl, tooltip = class))+
geom_bar()
p
})
output$modalText <- renderText({
selected_class <- input$mainplot_selected
paste("You have selected", selected_class, ".")
})
# open modal on plot click using _selected
observeEvent(input$mainplot_selected,
ignoreNULL = TRUE,
ignoreInit = TRUE,
showModal(plotModal(session))
)
}
shinyApp(ui, server)