Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to call a function on closing the server process in shiny?

I have a shiny application in which I am making some connections to databases and other components. I wish to close these connections when the app is brought down. Is there a way to execute a function when the shiny app is closed?

like image 764
Avinash Avatar asked Dec 25 '22 06:12

Avinash


1 Answers

As mentioned in the comments by @jdharrison you can us session$onSessionEnded in the shiny server.

This extremely simple example will print a message to the console when you close the app, but you can replace that print statement with some statements that close the database connections.

library(shiny)
ui <- fluidPage(
   #Empty UI
)

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

  session$onSessionEnded(function() {
    print('hello, the session has ended')
  })
}


shinyApp(ui = ui, server = server)
like image 62
aeongrail Avatar answered Dec 28 '22 07:12

aeongrail