[R] Missing arguments to new

John Chambers jmc at research.bell-labs.com
Fri Dec 19 21:59:57 CET 2003


J Swinton wrote:
> 
> Is this the expected and/or correct behaviour? And if
> so, how do I persuade new to interpret a named but
> missing argument as missing?

The general problem you're having has to do with understanding "...",
not with new(), but as it happens, there is a special mechanism for
calls to new() (there is a pointer in the online documentation for
new()).

You can't use the S language mechanism of missing(x) for arguments
matched as part of "..."--the language has no way to ask "is the actual
argument in ... corresponding to x missing".

There are several standard tricks to get the same effect, such as:
	args <- list(...)
	if(is.null(args$x)) # act as if "x" was missing

For generating objects from a class, there is a special mechanism that
is clearer and more intuitive for your users.

If you look at the R function new(), you'll see it ends in the call
  initialize(value, ...)
where `value' is a standard object from the class.

By defining a method for initialize() for the appropriate class, and
using an extended feature that allows methods to override the "..."
argument in the generic, you can end up with a method that checks
directly for missing arguments.

For your example class, something like the following:

setClass("testMissing",representation(a="numeric"),prototype=list(a=0))
setMethod("initialize", "testMissing", function(.Object, a){
    if(missing(a))message("Missing!")
    else .Object at a <- a
    .Object
})

which gives the following results (you can check for "a" whether named
in the call or not)

R> new("testMissing", a=1)
An object of class "testMissing"
Slot "a":
[1] 1

R> new("testMissing", 1)
An object of class "testMissing"
Slot "a":
[1] 1

R> new("testMissing")
Missing! 
An object of class "testMissing"
Slot "a":
[1] 0




> 
> -------------
> 
> setClass("testMissing",representation(a="numeric"),prototype=list(a=0))

;
> 
> showMissing <- function(real.arg,missing.arg) {
>   really.missing <- new("testMissing");
>   show(really.missing);
>   really.there <- new("testMissing",a=1);
>   show(really.there);
>   arg.there <- new("testMissing",a=real.arg);
>   show(arg.there);
>   arg.missing <- new("testMissing",a=missing.arg);
>   show(arg.missing);
> }
> 
> > showMissing(real.arg=2)
> An object of class "testMissing"
> Slot "a":
> [1] 0
> 
> An object of class "testMissing"
> Slot "a":
> [1] 1
> 
> An object of class "testMissing"
> Slot "a":
> [1] 2
> 
> Error in initialize(value, ...) : Argument
> "missing.arg" is missing, with no default
> 
> ----------
> 
> Jonathan Swinton
> 
> =====
> Jonathan Swinton; jonathan at swintons.net
> 
> ________________________________________________________________________
> Yahoo! Messenger - Communicate instantly..."Ping"
> your friends today! Download Messenger Now
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc




More information about the R-help mailing list