Re: [Rd] Missing values for S4 slots [One Solution]

From: Ross Boylan <ross_at_biostat.ucsf.edu>
Date: Fri 24 Nov 2006 - 21:18:55 GMT

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.



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat Nov 25 08:22:18 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 25 Nov 2006 - 08:30:44 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.