I have a small shiny app that executes an operation that is sometimes instantaneous and sometimes takes a few seconds. In the later case, I want to display a modal. The following works pretty well if the operation takes long; but it also flashes the modal for a few ms on the instant operation; which is pretty ugly.
library(shiny)
ui <- fluidPage(
# In the real app, there is only one operation that can either be fast or slow.
# The buttons in this example represent both possible scenarios.
actionButton("slowButton", "save slow"),
actionButton("fastButton", "save fast")
)
server <- function(input, output, session) {
observeEvent(input$slowButton, {
showModal(modalDialog("Saving..."))
Sys.sleep(3)
removeModal()
})
observeEvent(input$fastButton, {
# The modal should be suppressed here, because the operation is fast enough.
showModal(modalDialog("Saving..."))
Sys.sleep(0) # In reality, we do not know how long this takes.
removeModal()
})
}
shinyApp(ui, server)
Is there a way to add a delay to display the modal only if the operation takes longer than, let's say, half a second?