title: How to make variable available to namespace at loading time
body: <p>In one of my packages I use the <code>.onAttach</code> hook to run some R code and then use <code>assign</code> to make the value available as one of the package variables. I do it because <code>variable</code> depends on the content of some file, which can change between one session and the other. The code I use is like:</p>
  
  <pre><code>.onAttach &lt;- function(libname, pkgname) {
     variable &lt;- some_function()
     assign("variable", variable, envir = as.environment("package:MyRPackage"))
  }
  </code></pre>
  
  <p>When I attach the package with <code>library(MyRpackage)</code> I can use <code>variable</code>.</p>
  
  <p>However it is not possible to do something like <code>MyRPackage::variable</code> (unless I have already attached the package with <code>library(MyRpackage)</code>.</p>
  
  <p>I know this is because I should put that code in the <code>.onLoad</code> hook, however I can't make the assignment so that it works.</p>
  
  <p>I have tried</p>
  
  <pre><code>.onLoad &lt;- function(libname, pkgname) {
     variable &lt;- some_function()
     assign("variable", variable, envir = as.environment("namesoace:MyRPackage"))
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>.onLoad &lt;- function(libname, pkgname) {
     variable &lt;- some_function()
     assign("variable", variable, envir = asNamespace("MyRPackage"))
  }
  </code></pre>
  
  <p>but both of them fail with some error when I run <code>MyRPackage:::variable</code> without using <code>library</code> to attach the package.</p>
  
  <p>What is the correct to do the assignment in the .onLoad hook?</p>