How to stop running shiny app by closing the browser window?

前端 未结 3 979
猫巷女王i
猫巷女王i 2020-12-30 02:13

I have deployed an app in shinyapps.io and its working fine.

I am running the app for only 5 minutes, but when I checked the metrics it shows a run

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 03:14

    I'm not aware of shinyapps.io, but in R (as your tag shows) you can indeed stop a shinyApp through the onSessionEnded. The following 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()
    

提交回复
热议问题