How to stop the launch of a brilliant application by closing the browser window?

I deployed the application to shinyapps.io and it works fine.

I run the application in just 5 minutes, but when I checked the metrics, it shows a runtime of about 0.7 hours. I found that the default idle time is 15 minutes, which I changed to 5 minutes (minimum). I also noticed that even after closing the browser window of a brilliant application, it still shows that the application is working on my toolbar.

I assume that the application does not stop working when the browser window is closed and stops only when the downtime condition is met.

Is there a way to stop a brilliant application when the browser window is closed? Will the following code snippet work in this case?

session$onSessionEnded(function() {
    stopApp()
  })
+12
source share
3 answers

I don't know shinyapps.io, but in R (as your tag shows) you can really stop shinyAppthrough onSessionEnded. Below is a minimal working example.

rm(list=ls())

library(shiny)

doshiny <- function() {
  app=shinyApp(
    ui = fluidPage(
      textInput("textfield", "Insert some text", value = "SomeText")
    ),
    server = function(input, output, session) {
      session$onSessionEnded(function() {
        stopApp()
      })
    }
  )
  runApp(app)
}

openshiny <- function() {
  doshiny()
  print("Finished.")
}

openshiny()
+11
source

I found this great code that does this job. Essentially, you like it this way:

library(shiny)
library(shinyjs)

jscode <- "shinyjs.closeWindow = function() { window.close(); }"

ui <- fluidPage(
  useShinyjs(),
  extendShinyjs(text = jscode, functions = c("closeWindow")),
  actionButton("close", "Close window")
)

server <- function(input, output, session) {
  observeEvent(input$close, {
    js$closeWindow()
    stopApp()
  })
}

shinyApp(ui, server)

Please note that closing the browser window using JavaScript may be prohibited. But this is another discussion .

+6
source

I added this inactivityJS code to help me with some of my brilliant IDLE applications. The code pretty much speaks for itself, where I track mouse movements and clicks. Please note that this application will close after 5 seconds.

library(shiny)
library(leaflet)

inactivity <- "function idleTimer() {
  var t = setTimeout(logout, 5000);
  window.onmousemove = resetTimer; // catches mouse movements
  window.onmousedown = resetTimer; // catches mouse movements
  window.onclick = resetTimer;     // catches mouse clicks
  window.onscroll = resetTimer;    // catches scrolling
  window.onkeypress = resetTimer;  //catches keyboard actions

  function logout() {
    window.close();  //close the window
  }

  function resetTimer() {
    clearTimeout(t);
    t = setTimeout(logout, 5000);  // time is in milliseconds (1000 is 1 second)
  }
}
idleTimer();"


ui <- fluidPage(
  tags$script(inactivity),  
  actionButton("recalc","recalc"),
  leafletOutput("mymap")

)

server <- shinyServer(function(input,output,session){

  points <- eventReactive(input$recalc, {
    cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
  }, ignoreNULL = FALSE)

  output$mymap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$Stamen.TonerLite,options = providerTileOptions(noWrap = TRUE)) %>% 
      addMarkers(data = points())
  })

})
runApp(list(ui = ui, server = server))
+1
source

Source: https://habr.com/ru/post/1628055/


All Articles