I’m building a Shiny app where there are important clean-up operations to do when a user signs out.
I’ve been using session$onSessionEnded and/or onStop for this clean-up routine. It works beautifully if the user closes the window. But it fails if the user uses the sign out button in the navbar. I think it might be because the container in destroyed before the clean-up can be done, but I’m not sure if that’s really the case.
Simple reproducible example:
con <- dbConnect(RPostgres::Postgres(),
dbname = Sys.getenv("POSTGRES_DB"),
host = Sys.getenv("POSTGRES_URL"),
port = Sys.getenv("POSTGRES_PORT"),
user = Sys.getenv("POSTGRES_USER"),
password = Sys.getenv("POSTGRES_PW"))
ui <- bootstrapPage(
numericInput('n', 'Number of obs', n),
plotOutput('plot')
)
server <- function(input, output, session) {
output$plot <- renderPlot({
hist(runif(input$n))
})
session$onSessionEnded(function() {
sql <- "UPDATE bla
SET foo = NULL
WHERE foobar = 'foos'")
dbExecute(con, sql)
dbDisconnect(con)
})
onStop(function() {
sql <- "UPDATE bla
SET foo = NULL
WHERE foobar = 'foos'")
dbExecute(con, sql)
dbDisconnect(con)
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
Any assistance on this topic would be appreciated.