[Rd] as.missing
Charles C. Berry
cberry at tajo.ucsd.edu
Tue Oct 24 22:55:36 CEST 2006
On Tue, 24 Oct 2006, Duncan Murdoch wrote:
> 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.
Is this what you want?
> f <- function(fnodef, fdef=NULL) {
+ g()}
> f()
gnodef is missing
gdef is missing
gdef is 1
>
>
> Duncan Murdoch
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0717
More information about the R-devel
mailing list