From murdoch at stats.uwo.ca  Sun Nov 19 22:18:12 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Sun, 19 Nov 2006 16:18:12 -0500
Subject: [Rd] good practice for values not provided
In-Reply-To: <20061119204631.GA16883@pu100877.student.princeton.edu>
References: <20061119204631.GA16883@pu100877.student.princeton.edu>
Message-ID: <4560CA14.2050600@stats.uwo.ca>

On 11/19/2006 3:46 PM, Tamas K Papp wrote:
> Hi,
> 
> I am writing a collection of functions which I plan to share as a
> package later (when they are tested thoroughly), so I would like to do
> things "right" from the beginning...
> 
> I encountered a minor question of style.  Consider a function 
> 
> f <- function(a,b,x=NULL) {
>   ## ...
> }
> 
> if !is.null(x), f will use x to calculate the result, but if
> is.null(x), it will do something else not involving x at all (using
> any x would be meaningless here, so I can't use
> x=calcsomethingfrom(a,b)).
> 
> What's the accepted way of indicating this in R with a default for x?
> x=FALSE? x=NA? x=NULL?

I think the most common is x=NULL, but probably all of those are used in 
some package.  The advantage some default over no default and an 
is.missing(x) test, is that you can write g to call f with the same default:

g <- function(a,b,c,d,x=NULL) {
   f(a,b,x)
   # some more stuff
}

It would be harder if you wanted to signal missing x by not including it 
in the call, because you'd need a test in g.

Duncan Murdoch



[Rd] good practice for values not provided
Duncan Murdoch