title: Shiny Server - how to use session$onSessionEnded()
body: <p>I'm trying to track user activity as they go through my shiny app.  I have functions placed at specific locations which write lines to a temporary file.  What I want is to have a function which is called when the user session ends.</p>
  
  <p>According to the documentation:</p>
  
  <pre><code>&gt; ?session
  &gt; onSessionEnded(callback)  
        Registers a function to be called after the client has disconnected. Returns a function that can be called with no arguments to cancel the registration.
  </code></pre>
  
  <p>I tried to use this:</p>
  
  <pre><code>session$onSessionEnded(function(x){
    a = read_csv(shinyActivityTmpFile_000)
    write_csv(a,'C:\\Users\\xxxx\\Desktop\\test.csv')
  })
  </code></pre>
  
  <p>But nothing happened.  <code>shinyActivityTmpFile_000</code> is a global file reference.  As the user clicks buttons and does stuff, the app is writing out to this file in a CSV format.  I'm just trying to move it from temporary storage to permanent storage.  Ultimately I'd like the function to write it to a database but for testing I'm just trying to get a function that will run when the app is closed.</p>
  
  <p>What am I missing here?</p>