title: How to populate an environment in R
body: <p>I'm using the <code>IMIS</code> package (Incremental Mixture Importance Sampling) to estimate parameters. Unfortunately, it's written to look for functions <code>likelihood</code>, <code>sample.prior</code>, and <code>prior</code> in the environment it's called in, so I can't wrap it in a function (my end goal). The univariate example from <code>?IMIS</code> works just fine,</p>
  
  <pre><code>require(IMIS)
  likelihood &lt;- function(theta) exp(-1*sin(3*theta)*sin(theta^2) - 0.1*theta^2)
  prior &lt;- function(theta) dnorm(theta, 0, 5)
  sample.prior &lt;- function(n) rnorm(n, 0, 5)
  result = IMIS(500, 3000, 100, 10)
  
  ## also fine using do.call (pertinent below)
  result &lt;- do.call(IMIS, args = list(B = 500, B.re = 3000, number_k = 100, D = 10))
  </code></pre>
  
  <p>but unsurprisingly, wrapping it in a function does not:</p>
  
  <pre><code>rm(likelihood, prior, sample.prior, result)
  imisWrap &lt;- function() {
      likelihood &lt;- function(theta) exp(-1*sin(3*theta)*sin(theta^2) - 0.1*theta^2)
      prior &lt;- function(theta) dnorm(theta, 0, 5)
      sample.prior &lt;- function(n) rnorm(n, 0, 5)
      result = IMIS(500, 3000, 100, 10)
      return(result)
  }
  imisWrap() ## can't find sample.prior
  </code></pre>
  
  <p>I <em>think</em> the way around this is to create an environment in my wrapper (or use its environment) and then use <code>do.call</code> to run <code>IMIS</code> in that environment, but I don't know how to create a new environment that has <code>likelihood</code>, <code>prior</code>, <code>sample.prior</code>, and <code>result</code> in it.</p>
  
  <hr>
  
  <p><strong>Edit:</strong> Using @BenBolker's excellent comments I have an improved but still not working attempt:</p>
  
  <pre><code>imisWrap2 &lt;- function() {
      likelihood &lt;- function(theta) exp(-1*sin(3*theta)*sin(theta^2) - 0.1*theta^2)
      prior &lt;- function(theta) dnorm(theta, 0, 5)
      sample.prior &lt;- function(n) rnorm(n, 0, 5)
      imisEnv &lt;- new.env()
      assign("likelihood", likelihood, envir = imisEnv)
      assign("sample.prior", sample.prior, envir = imisEnv)
      assign("prior", prior, envir = imisEnv)
      result = do.call(IMIS,
                       args = list(B = 500, B.re = 3000, number_k = 100, D = 10),
                       envir = imisEnv)
      return(result)
  }
  </code></pre>
  
  <p>But this still can't find the functions.</p>