Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 running time for about 0.7 hours. I found that there is a default idle time of 15 minutes, which I have changed to 5 minutes (minimum). I also noticed that, even after closing the browser window of the shiny app, it still shows the app as running in my dashboard.

I assume that the app doesn't stop running when a browser window is closed and it will stop only when the idle time condition is met.

Is there a way to stop the shiny app when the browser window is closed? Would the following piece of code work in this instance?

session$onSessionEnded(function() {     stopApp()   }) 
like image 510
Eka Avatar asked Feb 10 '16 03:02

Eka


People also ask

How do you stop a Shiny app from running?

Automatically stop a Shiny app when closing the browser tabBy adding a single line to the server code session$onSessionEnded(stopApp) , a Shiny app will automatically stop running whenever the browser tab (or any session) is closed.

Why is the Shiny app so slow?

There are many reasons why a shiny app runs slower than expected. The most common reason is the Shiny app code has not been optimized. You can use the profvis package to help you understand how R spends its time. You also might want to make sure your server is large enough to host your apps.

Why does my app work locally but not on my Shiny server?

Your application may be dependent on packages that are installed and loaded in your environment, but aren't installed for the correct user on the Shiny Server. Make sure that all necessary packages are installed for the same user set under run_as in your Shiny Server configuration file.


2 Answers

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() 
like image 150
DoubleYou Avatar answered Oct 13 '22 08:10

DoubleYou


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.

like image 24
untill Avatar answered Oct 13 '22 09:10

untill