shiny
shiny copied to clipboard
removeModal() should have id parameter to remove specific modal
for example, you might have a modal dialog which opens when the app opens and is timed to close after 30 seconds via a timer which activates removeModal, but in the meantime the user has manually closed that modal and opened another, which then gets auto-closed by the timer, which is super annoying. I guess this might be hard as neither showModal and modalDialog themselves provide IDs.
Here is a workaround for this situation:
library(shiny)
ui <- fluidPage(
actionButton("open_importantModal", "open_importantModal")
)
server <- function(input, output, session) {
showModal(
modalDialog(
title = "Intro message",
"This is a intro message!", footer = actionButton("close_introModal", "Close")
)
)
introModal_opened_at <- as.numeric(Sys.time())
is_introModal_open <- reactiveVal(TRUE)
selfDestructiveObserver <- observe({
invalidateLater(500)
# removes introModal after 10s only if it is still existing
if(is_introModal_open() && as.numeric(Sys.time()) > introModal_opened_at+10){
removeModal()
selfDestructiveObserver$destroy()
}
})
observeEvent(input$close_introModal, {
removeModal()
is_introModal_open(FALSE)
selfDestructiveObserver$destroy()
})
observeEvent(input$open_importantModal, {
showModal(modalDialog(
title = "Important message",
"This is an important message!"
))
})
}
shinyApp(ui, server)