[Rd] Missing values for S4 slots

Ross Boylan ross at biostat.ucsf.edu
Fri Nov 24 20:23:14 CET 2006

Using R 2.4, the following fails:
setClass("testc", representation(a="ANY"))
makeC <- function(myarg) new("testc", a=myarg)
-> Error in initialize(value, ...) : argument "myarg" is missing,
   with no default

On the other hand, this is OK:
f <- function(a) g(b=a)
g <- function(b) if(missing(b)) "missing" else "valid arg"
-> "missing"

By the way, new("testc") succeeds.

Is there a good way around this?  Since my real example has several 4
arguments that might be missing, testing on missingness and calling
"new" with appropriate explicit arguments is not an attractive option.

Passing all the arguments in is also not an option, since my call to
"new" includes an argument not in the constructor (i.e., my equivalent
of makeC). 

I suspect there's something I could do to get the constructor
arguments, modify the list (i.e., delete args that were missing and
insert new ones), and do.call(new, myArgList).  Not only am I unsure
how to do that (or if it would work), I'm hoping there's a better way.

What's the best approach to this problem?

More information about the R-devel mailing list