I want to use a modal window inside a Shiny module. The user interacts with the modal window, the module processes the user's input.
In this minimal example the module is supposed to remove the modal when the user clicks the "close modal" button:
library(shiny)
# Modal module UI
modalModuleUI <- function(id) {
ns <- NS(id)
actionButton(ns("openModalBtn"), "Open Modal")
}
# Modal module server
modalModule <- function(input, output, session) {
myModal <- function() {
modalDialog(
actionButton("closeModalBtn", "Close Modal")
)
}
# Show modal dialog on start up
observeEvent(input$openModalBtn,
ignoreNULL = FALSE,
showModal(myModal())
)
# close modal on button click (not working)
observeEvent(input$closeModalBtn, {
removeModal()
})
}
# Main app UI
ui <- fluidPage(modalModuleUI("foo"))
# Main app server
server <- function(input, output, session) {
callModule(modalModule, "foo")
}
shinyApp(ui, server)
However, clicking the "close modal" button does not trigger the observeEvent()
in the module server function. I cannot figure out how to access (i.e. observe) the content of the modal window in the module. I guess it is a namespace issue.
Edit: The interactive example now works. See my answer below.