Is there a way when you close google chrome, your R is automatically closed?

1,365 views
Skip to first unread message

Ming

unread,
Oct 21, 2013, 5:20:58 PM10/21/13
to shiny-...@googlegroups.com
Hi,

I am using Rterm to launch my Shiny application (an Rterm shortcut on my desktop with the following target path, C:\Apps\software\R-3.0.1\bin\i386\Rterm.exe --file="run.R").  Every time I launch the application the black command window will show up and trigger the launch of google chrome; if I want to close the application, I have to close google chrome first, then I need to close the black command window as well.  I want to know if there is a way when you close the google chrome, the Rterm is automatically closed? Thanks.

Aqi

Winston Chang

unread,
Oct 21, 2013, 7:36:13 PM10/21/13
to Ming, shiny-...@googlegroups.com
This is a little bit of a hack, but it's possible to set the value of an input variable directly when the window is closed, but adding a beforeunload callback. For example:

runApp(list(
  ui = bootstrapPage(
    numericInput('n', 'Number of obs', 100),
    plotOutput('plot'),

    tags$script(type = "text/javascript", "
      $(function() { // Run when DOM ready
        $(window).bind('beforeunload', function(e) {
          Shiny.onInputChange('quit', true); // sets input$quit to true
        });
      });
   ")
  ),
  server = function(input, output) {
    output$plot <- renderPlot({ hist(runif(input$n)) })
    
    observe({
      # If input$quit is unset (NULL) do nothing; if it's anythign else, quit
      # and return input$n
      if (is.null(input$quit)) return()

      stopApp(input$n)
    })
  }
))

-Winston



--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jeff Allen

unread,
Oct 22, 2013, 11:49:34 AM10/22/13
to shiny-...@googlegroups.com, Ming
Alternatively, you could add a "stopApp()" call on a particular button. It's an extra step, but the code would be dead easy.

Finally, are you aware of Shiny Server? It will handle the startup and shutdown of R processes as needed -- so an R process would be spawned when you visited your app's URL, then be terminated once you closed the window (with no additional coding on your part). It looks like you're on Windows, which we don't yet support, so you'd likely need a virtual machine to get this running on your desktop. May be more trouble than it's worth for a single app, but if you find that you regularly are starting up and shutting down apps, it's something to keep in mind.

Jeff

Ming

unread,
Oct 22, 2013, 3:40:46 PM10/22/13
to shiny-...@googlegroups.com, Ming
Thanks, really helpful!

Ming

unread,
Oct 22, 2013, 3:41:01 PM10/22/13
to shiny-...@googlegroups.com, Ming
Thanks!!


On Monday, October 21, 2013 6:36:13 PM UTC-5, Winston Chang wrote:

Joe Cheng

unread,
Oct 22, 2013, 7:20:47 PM10/22/13
to Ming, shiny-...@googlegroups.com
How about:

shinyServer(function(input, output, session) {
  session$onSessionEnded(function() {
    q()
  })
})

Ming

unread,
Oct 22, 2013, 11:09:34 PM10/22/13
to shiny-...@googlegroups.com, Ming
This is exactly what I want, thanks a bunch!!

Vincent Nijs

unread,
Oct 28, 2013, 2:16:58 AM10/28/13
to shiny-...@googlegroups.com, Ming
This works very nicely.

Question: Is there a way to shiny to distinguish between (1) a browser refresh and (2) closing the browser? 

Joe Cheng

unread,
Oct 28, 2013, 3:11:32 PM10/28/13
to Vincent Nijs, shiny-...@googlegroups.com, Ming
No, and I don't think there's even a way in a webpage, period, to distinguish between the two. You could use a heuristic on the server side like "If another session isn't started within 1 second then it's closed", I suppose. I strongly suspect that's possible to implement using Shiny's reactive primitives but I don't have an implementation of that off the top of my head.

Vincent Nijs

unread,
Oct 28, 2013, 4:05:45 PM10/28/13
to shiny-...@googlegroups.com, Vincent Nijs, Ming
Thanks for the response Joe. I will use q('ask') for now. 
Reply all
Reply to author
Forward
0 new messages