From ross at biostat.ucsf.edu  Fri Nov 24 20:23:14 2006
From: ross at biostat.ucsf.edu (Ross Boylan)
Date: Fri, 24 Nov 2006 11:23:14 -0800
Subject: [Rd] Missing values for S4 slots
Message-ID: <20061124192314.GA16006@wheat.betterworld.us>

Using R 2.4, the following fails:
setClass("testc", representation(a="ANY"))
makeC <- function(myarg) new("testc", a=myarg)
makeC()
-> 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"
g()
-> "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?



[Rd] Missing values for S4 slots
Ross Boylan