title: R: Scoping error when calling function from script
body: <p>I am building a large codebase in R that uses vectorization through many nested user-defined functions. These auxiliary functions are, by in large, written in their own R script for debugging and maintenance purposes. I would like to source some of these scripts within a specific environment, without the functions being called into the Global Environment.</p>
  <p>I am trying to make the main.R file as clean and understandable as possible, so that there are scripts that do the <em>dirty work</em> behind the scenes.</p>
  <p>In the example below, the global environment becomes populated with <code>f_foobar</code> (which is correct), but also <code>f_foo</code> and <code>f_bar</code> once I call the function <code>f_foobar</code>.</p>
  <p>Is there an elegant way to house these auxiliary functions (eg <code>f_foo</code> and <code>f_bar</code>) in a temporary environment?</p>
  <p>For example, in file_foo.R:</p>
  <pre><code>f_foo &lt;- function() {
    return('a')
  }
  
  </code></pre>
  <p>In file_bar.R:</p>
  <pre><code>f_bar &lt;- function() {
    return('b')
  }
  </code></pre>
  <p>In file_foobar.R:</p>
  <pre><code>f_foobar &lt;- function() {
    source('file_foo.R')
    source('file_bar.R')
    
    value_foo &lt;- f_foo()
    value_bar &lt;- f_bar()
    
    c(value_foo, value_bar) %&gt;% return
  }
  </code></pre>
  <p>In main.R:</p>
  <pre><code>source('file_foobar.R')
  
  main_result &lt;-f_foobar()
  </code></pre>