stop waiter in shiny when interrupting execution R

If I stop this code (clic on red button in R studio or close cmd window executing it), I want the waiter to stop turning with the illusion that the code is still running. best case senario, when I close the cmd I want the shiny browser to close.

library(shiny)
library(waiter)

# Define UI
ui <- fluidPage(
  use_waiter(),  # Include waiter dependencies
  waiter_show_on_load(html = spin_3()),  # Show spinner on load
  titlePanel("Shiny App with Waiter"),
  sidebarLayout(
    sidebarPanel(
      actionButton("load", "Load Data")
    ),
    mainPanel(
      tableOutput("data")
    )
  )
)

# Define server logic
server <- function(input, output, session) {
  # Show waiter on load
  waiter <- Waiter$new()
  waiter$show()
  
  # Simulate data loading
  observeEvent(input$load, {
    Sys.sleep(2)  # Simulate a delay
    data <- head(mtcars)
    output$data <- renderTable(data)
    waiter$hide()  # Hide waiter after loading data
  })
}

# Run the application
shinyApp(ui = ui, server = server)

You could hide the waiter screen via JS once the app exits:

library(shiny)
library(waiter)

# Define UI
ui <- fluidPage(
  tags$script(HTML(
    "$(document).on('shiny:disconnected', function(event) {
      let collection = document.getElementsByClassName('waiter-overlay waiter-fullscreen');
      for (let i = 0; i < collection.length; i++) {
        collection[i].setAttribute('style', 'display:none');
      };
     });"
  )),
  use_waiter(),  # Include waiter dependencies
  waiter_show_on_load(html = spin_3()),  # Show spinner on load
  titlePanel("Shiny App with Waiter"),
  sidebarLayout(
    sidebarPanel(
      actionButton("load", "Load Data")
    ),
    mainPanel(
      tableOutput("data")
    )
  )
)

# Define server logic
server <- function(input, output, session) {
  # Show waiter on load
  waiter <- Waiter$new()
  waiter$show()
  
  # Simulate data loading
  observeEvent(input$load, {
    Sys.sleep(2)  # Simulate a delay
    data <- head(mtcars)
    output$data <- renderTable(data)
    waiter$hide()  # Hide waiter after loading data
  })
}

# Run the application
shinyApp(ui = ui, server = server)

Thank you it worked! Can you share with me some ressources to learn about these things, I've been looking everywhere for the solution? Is it just about learning javascript? and how to know how to mix it with R?

Please check the following links:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.