Rstudio Viewer window not closed on shiny::stopApp()

I use shiny::stopApp() in an application to do some cleanup to the R-environment when the user clicks a link to close an app (e.g., delete some functions and variables defined in global.R). This works fine when the app is run "external" in Chrome and Safari. However, when I use Rstudio viewer, the viewer window does not consistently close when shiny::stopApp() is called. Sometimes it clears and sometimes the greyed-out app is still shown, as is the "Reload app" button, even though the app has been stopped.

Two questions:

  1. Is there a way to force an Rstudio viewer window with a running Shiny app to close on shiny::stopApp()?

  2. The stop buttons in Rstudio do consistently stop the app and close the viewer window. However, when the user clicks the stop button the cleanup functions in my app won't be called. Is there a way, from inside a shiny app running in a viewer window, to "catch" when an Rstudio stop button is pressed by the user?


Any chance you can provide a reproducible example? Does this occur in all kinds of Shiny application you're developing, or just in a few? Is it possible that something is going wrong during the tear-down of the application requested by shiny::stopApp()?

It'd be helpful to know your operating system + version of RStudio as well.

I can reproduce the issue consistently. I don't have a small example (yet) however. Are there some Rstudio diagnostics that would be helpful? The lines of code that lead to the call to stopApp are shown below. As you can see from the screenshot, the code to executes cleanly. The required cleanup is done as well when I check the Environment.

message("\nStopped Radiant. State available as r_state and r_data.\n")

OS: macOS Sierra and Windows 10
Rstudio: 1.1.383 and 1.1.412
R: 3.4.2

Interestingly, I cannot reproduce the issue on Linux (CentOS 6.9, R 3.4.3, Rstudio Server Pro 1.1.402)

I'm not aware of any RStudio diagnostics that could be helpful here. What if you try to Right Click + Inspect Element within the Viewer pane -- do you see any JavaScript errors / messages in the console?

Thanks for the suggestion Kevin. Looks like there may be something here (see screenshot)

I'm not sure if this will make any difference, but there is a relatively new shiny::onStop() function that registers callback functions that are invoked when the application exits (when runApp() exits), or after each user session ends (when a client disconnects). Maybe using that function for all the cleanup and/or logging callbacks and then calling shiny::stopApp() could be worth a shot?

Re-reading, @vnijs, I actually think that the onStop() function is at least the answer to your second question:

Thanks @barbara! I initially thought the onStop function would produce the same result as onSessionEnded which I was already using. However, putting onStop in global.R made it possible to do cleanup when the stop button is pressed in Rstudio which is great. Thank you!

Using onStop, unfortunately, does not fix the issue of the viewer window in Rstudio not consistently closing when stopApp is called. I'll work on creating a small example where this happens. Any ideas about what could be going on with the JS error I posted?