[Rd] as.missing

Duncan Murdoch murdoch at stats.uwo.ca
Tue Oct 24 19:53:40 CEST 2006


On 10/24/2006 12:58 PM, Paul Gilbert wrote:
> (I'm not sure if this is a request for a feature, or another instance 
> where a feature has eluded me for many years.)
> 
> Often I have a function which calls other functions, and may often use 
> the default arguments to those functions, but needs the capability to 
> pass along non-default choices. I usually do this with some variation on
> 
> foo <- function(x, foo2Args=NULL or a list(foo2defaults),
>                     foo3Args=NULL or a list(foo3defaults))
> 
> and then have logic to check for NULL, or use the list in combination 
> with do.call.  It is also possible to do this with ..., but it always 
> seems a bit dangerous passing all the unnamed arguments along to all the 
> functions being called, especially when I always seem to be calling 
> functions that have similar arguments (maxit, eps, start, frequency, etc).
> 
> It is a situation I have learned to live with, but one of my 
> co-maintainers just pointed out to me that there should be a good way to 
> do this in R.  Perhaps there is something else I have missed all these 
> years?  Is there a way to do this cleanly? It would be nice to have 
> something like
> 
> foo <- function(x, foo2Args=as.missing(),  foo3Args=as.missing())
> 
> then the call to foo2 and foo3 could specify  foo2Args and foo3Args, but 
> these would get treated as if they were missing, unless they are given 
> other values.

I was going to say I couldn't see the difference between this and just 
declaring

  foo <- function(x, foo2Args, foo3Args)

with no defaults.  However, this little demo illustrates the point, I think:

 > g <- function(gnodef, gdef=1) {
+    if (missing(gnodef)) cat('gnodef is missing\n')
+    if (missing(gdef)) cat('gdef is missing\n')
+    cat('gdef is ',gdef,'\n')
+  }
 >
 >  f <- function(fnodef, fdef) {
+    g(fnodef, fdef)
+  }
 >
 >  g()
gnodef is missing
gdef is missing
gdef is  1
 >  f()
gnodef is missing
gdef is missing
Error in cat("gdef is ", gdef, "\n") : argument "fdef" is missing, with 
no default


What would be nice to be able to do is to have a simple way for f() to 
act just like g() does.

Duncan Murdoch




More information about the R-devel mailing list