From ross at biostat.ucsf.edu  Fri Nov 24 22:18:55 2006
From: ross at biostat.ucsf.edu (Ross Boylan)
Date: Fri, 24 Nov 2006 13:18:55 -0800
Subject: [Rd] Missing values for S4 slots [One Solution]
In-Reply-To: <20061124192314.GA16006@wheat.betterworld.us>
References: <20061124192314.GA16006@wheat.betterworld.us>
Message-ID: <20061124211854.GH4431@wheat.betterworld.us>

On Fri, Nov 24, 2006 at 11:23:14AM -0800, Ross Boylan wrote:
> 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
....
> 
> 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.

I didn't find a way to get all the arguments easily(*), but manually
constructing the list works.  Here are fragments of the code:

mspathCoefficients <- function(
                   aMatrix,
                   params,
                   offset=0,
                   baseConstrVec, 
                   covLabels # other args omitted
                   ) {
  # innerArgs are used with do.call("new, innerArgs)
  innerArgs <- list(
                    Class = "mspathCoefficients",
                    aMatrix = aMatrix,
                    baseConstrVec = as.integer(baseConstrVec),
                    params = params
                    )

# the next block inserts the covLabels argument
# only if it is non-missing
  if (missing(covLabels)) {
    # ....
  } else {
    innerArgs$covLabels <- covLabels
  }
#...
  map <- list() 
# fill in the map
# add it to the arguments
  innerArgs$map <- map
  do.call("new", innerArgs)
}

This calls new("mspathCoefficients", ...) with just the non-missing
arguments.  The constructed object has appropriately "missing" values
in the slots (e.g., character(0) given a slot of type "character").


(*) Inside the function, arg <- list(...) will capture the unnamed
arguments, but I don't have any.  as.list(sys.frame(sys.nframe()) is
closer to what I was looking for, though all the values are promises.



[Rd] Missing values for S4 slots [One Solution]
Ross Boylan