title: Evaluate an expression into a given environment
body: <p>I would like to call a function inside a given environment so that the execution of the function does not mess the global environment. I have something like that:</p>
  
  <pre><code>f &lt;- function() { x &lt;&lt;- 5 }
  e &lt;- new.env()
  evalq(f(), envir = e)
  </code></pre>
  
  <p>I want to evaluate <code>f()</code> inside the environment <code>e</code>, but it seems it does not work since the variable <code>x</code> is accessible from the global env instead of the env <code>e</code>. I mean I want to access <code>x</code> by typing <code>e$x</code>, not <code>x</code>.</p>
  
  <p>I tried to build the function <code>f</code> inside <code>e</code> like this:</p>
  
  <pre><code>e &lt;- new.env()
  evalq(f &lt;- function() { x &lt;&lt;- 5 }, envir = e) 
  evalq(f(), envir = e)
  </code></pre>
  
  <p>But it still does not work, <code>x</code> is in the global env, not in <code>e</code>.</p>
  
  <hr>
  
  <p>EDIT with more informations:</p>
  
  <p>From the answers I received, I understand that <code>&lt;&lt;-</code> should not be used. The problem is that, in practice, I don't know what <code>f</code> is, since it is obtained by sourcing a file which is downloaded through a shiny app. The purpose of this app is to evaluate a classifier function <code>f</code> written by students. So in case they used the <code>&lt;&lt;-</code> operator in their function, I don't want to pollute my global env (which seem to be impossible from the description of the <code>&lt;&lt;-</code> operator).</p>
  
  <p>I think I could remove the potentially created variables with something like</p>
  
  <pre><code>vars &lt;- ls()
  evalq(f(), envir = e)
  new_vars &lt;- ls()
  rm(list = new_vars[!new_vars %in% vars])
  </code></pre>
  
  <p>But is there a better solution?</p>