title: What are the suggested practices for function polymorphism in R?
body: <p>Suppose I want to write a function in <code>R</code> which is a function of a couple of sufficient statistics on some data. For example, suppose the function, call it <code>foo.func</code> depends only on the sample mean of a sample of data. For convenience, I think users might like to pass to  <code>foo.func</code> the sample of random variables (in which case <code>foo.func</code> computes the sample mean),  <em>or</em> the sample mean itself, which is all that <code>foo.func</code> needs. For reasons of efficiency, the latter is preferred if there are multiple functions like <code>foo.func</code> being called which can take the sample mean. In that case the mean need only be computed once (in the real problem I have, the sample statistics in question might be computationally intensive).</p>
  
  <p>In summary, I would like to write <code>foo.func</code> to be accessible to the beginner (pass in the data, let the function compute the sufficient statistics) as well as the expert (precompute the sufficient statistics for efficiency and pass them in). What are the recommended practices for this? Do I have a logical flag passed in? Multiple arguments? Some ways to do it might be:</p>
  
  <pre><code>#optional arguments
  foo.func &lt;- function(xdata, suff.stats=NULL) {
    if (is.null(suff.stats)) {
      suff.stats &lt;- compute.suff.stats(x)
    }
    #now operate on suff.stats
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>#flag input
  foo.func &lt;- function(data.or.stat, gave.data=TRUE) {
    if (gave.data) {
      data.or.stat &lt;- compute.suff.stats(data.or.stat)
    }
    #now operate on data.or.stat
  }
  </code></pre>
  
  <p>I am leaning towards the former, I think</p>