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

前端 未结 3 978
猫巷女王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:05

    I found this excellent code which does the job. Basically, you do like so:

    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)
    

    Note though that closing the browser window through JavaScript may be prohibited. But this is another discussion.

提交回复
热议问题