title: How to prevent functions polluting global namespace?
body: <p>My R project is getting increasingly complex, and I'm starting to look for some construct that's equivalent to classes in Java/C#, or modules in python, so that my global namespace doesn't become littered with functions that are never used outside of one particular .r file.</p>
  
  <p>So, I guess my question is: to what extent is it possible to limit the scope of functions to within a specific .r file, or similar?</p>
  
  <p>I think I can just make the entire .r file into one giant function, and put functions inside that, but that messes with the echoing:</p>
  
  <p>myfile.r:</p>
  
  <pre><code>myfile &lt;- function() {
      somefunction &lt;- function(a,b,c){}
      anotherfunction &lt;- function(a,b,c){}
  
      # do some stuff here...
      123
      456
      # ...
  }
  myfile()
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&gt; source("myfile.r",echo=T)
  
  &gt; myfile &lt;- function() {
  +     somefunction &lt;- function(a,b,c){}
  +     anotherfunction &lt;- function(a,b,c){}
  + 
  +     # do some stuff here...
  +     # . .... [TRUNCATED] 
  
  &gt; myfile()
  &gt; 
  </code></pre>
  
  <p>You can see that "123" is not printed, even though we used <code>echo=T</code> in the <code>source</code> command.</p>
  
  <p>I'm wondering if there is some other construct which is more standard, since putting everything inside a single function doesn't sound like something that is really standard?  But perhaps it is?  Also, if it means that <code>echo=T</code> works then that is a definite bonus for me.</p>