Detect User Initiated Refresh

Hi All,

Currently I'm having to deploy my app locally for users (creating a batch file for them to launch the app) and want to insert code that will detect how many sessions they have open and only stop the app if they close all of their sessions.

Below I have my current example code that I'm using to test the idea. The session tracking code was copied from here: https://gist.github.com/trestletech/9926129

At the end, is where I"m adding code to try and control when the app shuts down. It works as expected, except for when the user reloads the browser. I was wondering if there was anyway to detect when the user initiates a page reload, and prevent the app from stopping in that case?

Thanks!

library(shiny)

ui <- fluidPage(
  titlePanel("Session Counter"),

  mainPanel(
    "There are currently", 
    verbatimTextOutput("count"),
    "session(s) connected to this app."
  )
)

vals <- reactiveValues(count=0)

server <- function(input, output, session) {
  isolate(vals$count <- vals$count + 1)
  
  session$onSessionEnded(function(){
    isolate(vals$count <- vals$count - 1)
  })
  
  output$count <- renderText({
    vals$count
  })
  
  ###Added by myself
  observe(
    if(vals$count==1){
      session$onSessionEnded(stopApp)
    }
  )
}

shinyApp(ui, server)
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.