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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With