Search code examples
rshinyshinyalert

Reload page when upload complete


I have a shiny app for uploading CSV file. I used the function sendSweetAlert from shinyWidgets package to send custom message but I would like to reload the page when user clik on "Ok" button

enter image description here

I tried to use shinyalert from shinyalert package but i dont get the desired result.

This is what I did :

library(shiny)
library(shinyWidgets)
library(shinyalert)


ui <- fluidPage( 
  
  fileInput("file1", "Choose CSV File",
            multiple = FALSE,
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv"),
            width = "80%")
)

server <- function(input, output,session) {
  
  # a progress bar
  observeEvent(
    input$file1,
    {  withProgress(message = "Progress...",value = 0, {
      for (i in seq_len(50)) {
        Sys.sleep(0.1)
        incProgress(1 / 50)
        
        setProgress(NULL, detail = paste(round(((i+1)*100/50),digits = 0),"%"))
      }
      
      setProgress(1, detail = "100%")
      
      # the custom message when progress reach 100%
      sendSweetAlert(
        session = session,
        title = "Great",
        text = "Done !",
        type = "success"
      )
      
      # my second attempt but doest work
      
      # shinyalert::shinyalert('Succes', callbackR = mycallback)
      # mycallback <- function(value) {
      #   "javascript:window.location.reload(true)"
      # }
      
      
    })}
  )
  
}

shinyApp(ui,server)

Some help would be appreciated


Solution

  • You can use callbackJS in shinyalert():

    server <- function(input, output,session) {
      
      # a progress bar
      observeEvent(
        input$file1,
        {  withProgress(message = "Progress...",value = 0, {
          for (i in seq_len(50)) {
            Sys.sleep(0.1)
            incProgress(1 / 50)
            
            setProgress(NULL, detail = paste(round(((i+1)*100/50),digits = 0),"%"))
          }
          
          setProgress(1, detail = "100%")
          
    
          shinyalert::shinyalert('Succes', callbackJS = "function() {location.reload()}")
          
        })}
      )
      
    }
    

    With this, the page will reload when you click on "Ok" in the modal.