title: How should one declare Shiny variables that are shared across all functions?
body: <p>There are two ways in which a variable can be accessed across all server functions in a Shiny app.</p>
  <p>One would be defining it inside the server, and then accessing it with &lt;&lt;- operator. It does <strong>not</strong> become shared across all sessions when doing so. Not being shared across all sessions is the intended behavior.</p>
  <pre><code>library(shiny)
  
  ui &lt;- fluidPage(
      actionButton(&quot;btn&quot;, &quot;Increase num&quot;),
      actionButton(&quot;msg&quot;, &quot;Show num&quot;)
  )
  
  server &lt;- function(input, output) {
      n &lt;- 0
      observeEvent(input$btn, { n &lt;&lt;- n+1 } )
      observeEvent(input$msg, { showModal(modalDialog(title=n, easyClose = T)) } )
  }
  shinyApp(ui = ui, server = server)
  </code></pre>
  <p>The other would be creating a reactive variable</p>
  <pre><code>library(shiny)
  ui &lt;- fluidPage(
      actionButton(&quot;btn&quot;, &quot;Increase num&quot;),
      actionButton(&quot;msg&quot;, &quot;Show num&quot;)
  )
  
  server &lt;- function(input, output) {
      n &lt;- reactiveVal(0)
      observeEvent(input$btn, { n(n()+1) } )
      observeEvent(input$msg, { showModal(modalDialog(title=n(), easyClose = T)) } )
  }
  shinyApp(ui = ui, server = server)
  </code></pre>
  <p>Which one would be better, and why?</p>