Is there a way to call a function on closing the server process in shiny?
r, rstudio, shiny, shiny-server
Solution
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)
Problem
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?