Reputation: 5958
I want to reload my session periodically in my shiny app, which works well with session$reload()
.
I also want the app to be terminated when the user quits chrome, which works well using session$onEnded(stopApp)
Excepted that now the app stops when I reload. I tried fixing this using session$onSessionEnded(stopApp)
without success. Removing one of those statements fixes the issue.
Any idea on using session$reload()
without causing session$onEnded(stopApp)
to terminate?
Upvotes: 0
Views: 842
Reputation: 21
You can use a shiny action button to trigger the session$reload, and isolate stopApp()
to run only if the action button has not been triggered (such as an exit action button or closing browser tab). The only caveat is that if you reload/refresh the shiny app with the browser's refresh button or command it will crash the app.
library(shiny)
library(shinyjs)
ui <- fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(
text = "shinyjs.exit = function() { window.close(); }",
functions = c("exit")),
actionButton(
inputId = "exit_button",
label = "Exit",
width = "50%"
),
actionButton(
inputId = "reset_button",
label = "Reset",
width = "50%"
),
textOutput("reset_val")
)
server <- function(input, output, session){
reset_rv <- reactiveVal(value = 0L)
output$reset_val <- renderText({
paste0("Reset button value: ", reset_rv())
})
observeEvent(input$exit_button, {
shinyjs::js$exit()
session$onSessionEnded(function(){
stopApp()
})
})
observeEvent(input$reset_button, {
reset_rv(input$reset_button)
session$reload()
})
session$onSessionEnded(function(){
x <- isolate(reset_rv())
if(x == 0) {
stopApp()
}
})
}
runApp(
appDir = shinyApp(
ui = ui,
server = server
),
launch.browser = TRUE
)
Upvotes: 2