<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/stop-shiny-and-close-browser-together/"></div></div><div><a data-url="http://www.r-bloggers.com/stop-shiny-and-close-browser-together/" data-counturl="http://www.r-bloggers.com/stop-shiny-and-close-browser-together/" data-count="horizontal" data-text="Stop Shiny and Close Browser Together" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://data-steve.github.io/shiny-shutdown-with-browser-close/"> data_steve</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img src="http://i2.wp.com/data-steve.github.io/images/shutdown.png?w=240" data-recalc-dims="1"></p>
<p>I’ve been playing with <strong>shiny</strong> at work for a project <a target="_blank" href="https://github.com/trinker/">Tyler</a> and I have been pushing forward.</p>
<h4>My Goal</h4>
<p>We wanted to be able to deploy local instances of shiny app and then when user is done with session, the user can close out the session by closing the browser and shutting down shiny server at same time.</p>
<p>StackOverflow and Google groups had several stepping stones to help in the process, which with a bit of refactoring did the trick. The <a target="_blank" href="http://stackoverflow.com/questions/35306295/how-to-stop-running-shiny-app-by-closing-the-browser-window">first link</a> pointed to the javascript snippet I could possibly use, though no real suggestions on how to use it.</p>
<div>
<pre><code>session$onSessionEnded(function() {
    stopApp()
})
</code></pre>
</div>
<p><a target="_blank" href="https://groups.google.com/forum/#!searchin/shiny-discuss/insert$20javascript/shiny-discuss/F3GOgIJnRVs/iXfSqSCmCgAJ">Next</a>, shiny’s google group had a tip on inserting Javascript into shiny, which didn’t work for me but evidently did for others. Then, <a target="_blank" href="http://stackoverflow.com/questions/24526403/timing-events-when-session-ends/24646283#24646283">back at StackOverflow</a>, I found that <strong>the javascript above has its drawbacks if a user refreshed their session midway.</strong></p>
<h4>My Solution and Edit</h4>
<p>The solution that works for me in this minimal shinyApp demo was to edit the SO answer at the last link. (At time of this post, my edit had not been approved yet, so you may find the SO link has code that doesn’t work.) Basically, just add a close button that shuts down shiny server locally and closes browser, simple enough. And in shinyUI, one could possibly make this button appear only once end of app session was complete.</p>
<p>I had to edit the original answer because I couldn’t get the proposed solution to <code>observe</code> the <code>stopApp()</code> command before <code>window.close()</code> closed the connection to shiny. So, I added a simple <code>setTimeout</code> solution around the <code>window.close();</code> action to give shiny enough time to <code>observe</code>, combining this <a target="_blank" href="http://stackoverflow.com/questions/951021/what-do-i-do-if-i-want-a-javascript-version-of-sleep">SO absolute minimal clue of an answer</a> with <a target="_blank" href="http://www.w3schools.com/jsref/met_win_settimeout.asp">good ol’ W3’s practice session example</a>.</p>
<div>
<pre><code>library(shiny)
runApp(list(
  ui = bootstrapPage(
    tags$button(
      id = 'close',
      type = &quot;button&quot;,
      class = &quot;btn action-button&quot;,
      onclick = &quot;setTimeout(function(){window.close();},500);&quot;,  # close browser
      &quot;Close window&quot;
    )
  ),
  server = function(input, output) {
    observe({
      if (input$close > 0) stopApp()                             # stop shiny
    })
  }
))
</code></pre>
</div>
<p>I thought I’d write all this up in one place so that all my running around wasn’t a waste.</p>
<p><a target="_blank" class="twitter-share-button" data-via="data_steve" data-size="large" data-hashtags="rstats,rshiny,js,datascience" data-dnt="true" href="https://twitter.com/share">Tweet</a></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/stop-shiny-and-close-browser-together/"></div></div><div><a data-url="http://www.r-bloggers.com/stop-shiny-and-close-browser-together/" data-counturl="http://www.r-bloggers.com/stop-shiny-and-close-browser-together/" data-count="vertical" data-text="Stop Shiny and Close Browser Together" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://data-steve.github.io/shiny-shutdown-with-browser-close/"> data_steve</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/qu2iXqzOooU"></a></div>