title: Writing functions in R, keeping scoping in mind
body: <p>I often write functions that need to see other objects in my environment.  For example:</p>
  
  <pre><code>&gt; a &lt;- 3
  &gt; b &lt;- 3
  &gt; x &lt;- 1:5
  &gt; fn1 &lt;- function(x,a,b) a+b+x
  &gt; fn2 &lt;- function(x) a+b+x
  &gt; fn1(x,a,b)
  [1]  7  8  9 10 11
  &gt; fn2(x)
  [1]  7  8  9 10 11
  </code></pre>
  
  <p>As expected, both these functions are identical because <code>fn2</code> can "see" a and b when it executes.  But whenever I start to take advantage of this, within about 30 minutes I end up calling the function without one of the necessary variables (e.g. a or b).  If I don't take advantage of this, then I feel like I am passing around objects unnecessarily.</p>
  
  <p>Is it better to be explicit about what a function requires?  Or should this be taken care of via inline comments or other documentation of the function?  Is there a better way?</p>