R crashes when closing Shiny app window, instead of clicking red stop button in RStudio

When developing and testing Shiny apps, I've noticed that if I forget and close the RStudio-created shiny app window, R will hang, requiring terminating and restarting R.

This doesn't happen if I actually remember to go to the RStudio app and click the red stop button. But, that's a hassle, because I'm testing the App, so the App window is where my focus is.

Would it be possible to have R terminate more gracefully if the Shiny app window is closed?

This should help you :

library(shiny)

ui <- fluidPage()

server <- function(input, output, session) {
  
  session$onSessionEnded(function() {
    stopApp()
  })
  
}

shinyApp(ui, server)
2 Likes

Thank you -- on a quick test, that seems to work perfectly.

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.