title: R - How do we load a library in a Shiny App so that functions are local, so functions can modify the session specific variable using <<- operator?
body: <p><strong>How do we load a library in a Shiny App such that its functions are local to the session, such that the library is loaded for each session</strong>. The reason for that is we have a package XYZ it has function Test, which sets the value of a global variable "sessionVariable" to 2. Now if we source the XYZ directly in shinyServer the sessionVariable is session specific and everything works well. But when we build XYZ into a package and use the library function it looks for the sessionVariable  in R's global environment and not in shinyServer session and it fails. <strong>How can we do something like  source("../R/XYZ.R",local=TRUE) replace it with library(XYZ, local=TRUE)</strong></p>
  
  <p>The reason we want to do this are</p>
  
  <ol>
  <li>sessionVariable has to be a session variable only visible in the server</li>
  <li>The XYZ package has functions that are all aware of sessionVariable since when XYZ was developed the sessionVariable always was a global variable and we wanted to prevent passing the sessionVariable to each function to prevent overhead.</li>
  <li>When we use source the sessionVariable acts as pseudo Global as in its specific to server session ( &lt;&lt;- operator still works (within functions in XYZ.R only modifies session specific copy of sessionVariable) and XYZ.R functions work without any problem <strong><em>and when we end / exit the shiny app. There is no global sessionVariable in R environment (proving the point its session specific).</em></strong></li>
  <li>Using library seems elegant as package can be a binary build. I understand this is problem of elegant solution rather than a problem with no solution.</li>
  </ol>
  
  <p><strong>Example using Source</strong></p>
  
  <pre><code>library("shiny") 
  
  shinyServer(function(input, output) {
  getEnvOf &lt;- function(what, which=rev(sys.parents())) { for (frame in which) if (exists(what, frame=frame, inherits=FALSE)) return(sys.frame(frame)) return(NULL) }
  source("../R/XYZ.R",local=TRUE)
  sessionVariable &lt;- 1
  print(getEnvOf("sessionVariable"))
  print (sessionVariable)
  testFunc()
  print(getEnvOf("testFunc"))
  print(sessionVariable)
  }
  </code></pre>
  
  <p>in XYZ.R</p>
  
  <pre><code>testFunc &lt;-  function()
  {
  sessionVariable &lt;&lt;- 2
  }
  </code></pre>
  
  <p><strong>Results :</strong></p>
  
  <pre><code>&lt;environment: 0x0000000055138738&gt;
  &lt;environment: 0x0000000055138738&gt;
  function()
  {
  .... 
  }
  &lt;environment: 0x0000000055138738&gt;
  </code></pre>
  
  <p>Results would be 1 2</p>
  
  <p><strong>Example using library</strong></p>
  
  <p>But if I binary build XYZ.R</p>
  
  <p>And do this </p>
  
  <pre><code>library("shiny") 
  
  shinyServer(function(input, output) {
  getEnvOf &lt;- function(what, which=rev(sys.parents())) { for (frame in which) if (exists(what, frame=frame, inherits=FALSE)) return(sys.frame(frame)) return(NULL) }
  library("XYZ")
  sessionVariable &lt;- 1
  print(getEnvOf("sessionVariable "))
  print (sessionVariable)
  testFunc()
  print(getEnvOf("testFunc"))
  print(sessionVariable)
  }
  </code></pre>
  
  <p><strong>Results :</strong></p>
  
  <pre><code>&lt;environment: 0x0000000055138738&gt;
  NULL
  function()
  {
  .... 
  }
  &lt;environment: namespace:XYZ&gt;
  </code></pre>
  
  <p><em>I get 1 and error sessionVariable not found in testFunc</em></p>
  
  <p>I am guessing the library loads in Global environment and sessionVariable scope is within shinyServer and hence it doesn't work. Offcourse I can change the line sessionVariable &lt;- 1 to sessionVariable &lt;&lt;- 1 and make sessionVariable global but that's not what I want to do.</p>
  
  <p><strong>Alternatively I also tried using import::here</strong></p>
  
  <p>Using third alternate (to source and library) i.e. using import::here("testFunc",.from="XYZ") and checking the env of testFunc using getEnvOf shows the the env of testFunc and sessionVariable is the same </p>
  
  <pre><code>library("shiny") 
  
  shinyServer(function(input, output) {
  library("XYZ")
  getEnvOf &lt;- function(what, which=rev(sys.parents())) { for (frame in which) if (exists(what, frame=frame, inherits=FALSE)) return(sys.frame(frame)) return(NULL) }
  sessionVariable &lt;- 1
  print(getEnvOf("sessionVariable"))
  print (sessionVariable)
  testFunc()
  print(getEnvOf("testFunc"))
  print(sessionVariable)
  }
  </code></pre>
  
  <p><strong>Results :</strong></p>
  
  <pre><code>&lt;environment: 0x00000000443d03a0&gt;
  &lt;environment: 0x00000000443d03a0&gt;
  function () 
  { ...
  }
  &lt;environment: namespace:XYZ&gt;
  </code></pre>
  
  <p>Which also doesn't work as I get the same error as using library.</p>