title: How to access package variable from function?
body: <p>How can I access a package variable from a function in the same package?</p>
  
  <p>The following code only changes <code>x</code> locally to the function. How can I change <code>myPackage::x</code> from that function?</p>
  
  <p>Example:</p>
  
  <pre><code>x &lt;- list()
  populate_list &lt;- function() {
      x["a"] &lt;- 4
  }
  </code></pre>
  
  <p>--</p>
  
  <p>Here is the full code I'm using to test this with <code>get</code>. The <code>cache</code> key is simply chosen to be different every time the package is run. This is the package code, in <code>R/get_obj.R</code></p>
  
  <pre><code>cache &lt;- list()
  test_cache &lt;- function(){
      cache &lt;- get("cache")
      cache[[paste(sample.int(10),collapse="-")]] &lt;- 1
      return(cache)
  }
  </code></pre>
  
  <p>I test it in R with:</p>
  
  <pre><code>&gt; library(devtools)
  &gt; load_all("mypackage")
  Loading mypackage
  &gt; mypackage::test_cache()
  $`4-1-6-5-9-3-10-2-7-8`
  [1] 1
  
  &gt; mypackage::test_cache()
  $`6-9-7-10-5-1-4-2-3-8`
  [1] 1
  
  &gt; mypackage::cache
  list()
  </code></pre>
  
  <p>For <code>mypackage::cache</code> and the second run of <code>mypackage::test_cache()</code>, I expected this list as output:</p>
  
  <pre><code>$`4-1-6-5-9-3-10-2-7-8`
  [1] 1
  
  $`6-9-7-10-5-1-4-2-3-8`
  [1] 1
  </code></pre>