Skip to content

Dismiss modal after CallbackR finished #29

@dipterix

Description

@dipterix

Background

callbackR function is executed after modal being dismissed. One potential problem is if callbackR takes seconds to run, the Shiny apps will freeze up.

Simple example

library(shiny)

ui <- fluidPage(
  shinyalert::useShinyalert(),
  actionButton('a','Ok')
)

server <- function(input, output, session) {
  observeEvent(input$a, {
    shinyalert::shinyalert(
      'Warning',
      'Click OK to run code',
      callbackR = function(...){
        # procedure runs for 3~4 seconds
        Sys.sleep(3 + runif(1))
      }
    )
  })
}

shinyApp(ui, server)

If you dismiss the modal and immediately press the "Ok" button, the app freezes around 3 seconds before opening the modal again. This greatly affects user's experience and making callbackR less attractive.

My Solution

I modified your code a little bit, added a function called dismissalert to manually remove messages. I'll submit a PR soon, here is my forked repo. Basically I modified your JS code, and added R function dismissalert to remove arbitrary number of modals.

The working example looks like this:

library(shiny)

ui <- fluidPage(
  shinyalert::useShinyalert(),
  actionButton('a','Ok')
)

server <- function(input, output, session) {
  observeEvent(input$a, {
    shinyalert::shinyalert(
      'Warning',
      'Click OK to run code',
      callbackR = function(...){
        # procedure 1 runs for 3~4 seconds
        Sys.sleep(3 + runif(1))
        # Now dismiss modal 
        shinyalert::dismissalert() # <------------- new function to remove alerts
      }
    )

    # A second modal right after the first one, once user 
    # press OK button, pop up message and show the code is running
    # Once finished, remove all alerts.
    shinyalert::shinyalert(
      'Scheduled!',
      'R is running, please wait for the result...', 
      showConfirmButton = FALSE
    )
  })
}

shinyApp(ui, server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions