Grits
Grits

Reputation: 163

Loading message while system function is running R

My Shiny application has a system function that can take a while. I would like to display a message like "Loading ..." or a loading spin when my application proceeds to the function. Once finished, I would like the animation to disappear, or display a validation. My function looks like this :

observeEvent(input$send, {

    shinyalert("Your request has been sent! Please wait. An email will be sent to you in the next few minutes.",type = "success")


    #-------- SYSTEM FUNCTION --------#
    sys <- paste('/home/user/sen2extract/blabla.sh ',
                 shpWGS$V1,' ',
                 paste(input$indice, collapse = ','), ' ',
                 input$dates[1], ' ',
                 input$dates[2],
                 sep = "")
    system(sys)

Upvotes: 1

Views: 275

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84559

Something like that:

library(shiny)
library(shinyjs)

css <- "
.busy { 
  position: fixed;
  z-index: 1000;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -50px;
  display: none;
  background-color: rgba(230,230,230,.8);
  text-align: center;
  padding-top: 20px;
  padding-left: 30px;
  padding-bottom: 40px;
  padding-right: 30px;
  border-radius: 5px;
}"

ui <- fluidPage(
  useShinyjs(),
  tags$head(tags$style(css)),
  tags$div(class = "busy", 
           tags$img(src = "https://loading.io/spinners/comets/lg.comet-spinner.gif")),
  actionButton("go", "Go")
) 

server <- function(input, output, session){

  observeEvent(input$go, {
    runjs("$('.busy').show();")
  }, priority = 1, ignoreInit = TRUE)

  observeEvent(input$go, {
    Sys.sleep(5)
    runjs("$('.busy').hide();")
  }, ignoreInit = TRUE)
}

shinyApp(ui, server)

I use Sys.sleep to simulate your system(sys).

Upvotes: 1

Related Questions