title: How to reuse code in an R function?
body: <p>I have a block of code that I want to use several times inside a function (let's call it <code>myFunction</code>). I naturally want to avoid duplicating this block of code, but I can't find a way of reusing it short of putting it in an external file and sourcing that each time.</p>
  
  <p>The first thing I tried was to put the duplicate code in an internal mini-function with no arguments (let's call it <code>internalFunction</code>. This meant that I could call <code>internalFunction</code> as needed; however, this masked the objects output by <code>internalFunction</code> from the main environment of <code>myFunction</code>. </p>
  
  <p>I then tried using the <code>&lt;&lt;-</code> operator to assign output objects within <code>internalFunction</code>, so that they would be made available to the main environment of the <code>myFunction</code>. Unfortunately, this also makes those objects available to the global R environment outside <code>myFunction</code>, which I want to avoid.</p>
  
  <p>Is there a way of writing a block of R code to an object and then calling that, or sourcing from an object instead of a file? I would really like to a) avoid duplicate code and b) include all code within a single file.</p>