Skip to content
Prev 23005 / 63424 Next

Missing values for S4 slots [One Solution]

Ross Boylan <ross at biostat.ucsf.edu> writes:
The following might offer one paradigm:

makeC <- function(myarg) {
    new("testc",
        a=if (missing(myarg)) character(0) else myarg)
    }

A couple of things that come to mind, though: 'ANY' kind of defeats
the purpose of having a class with well-defined slots (I realize this
is a 'toy' example); presenting myarg without a default makes it
obscure to the user what the default value might be, or that a default
value will be used; if 'myarg' is an appropriately descriptive
argument for the user, then perhaps it is also appropriately
descriptive for the slot. Thus:

setClass("testc",
         representation(myarg="character"))

makeC <- function(myarg=character(0)) new("testc", myarg=myarg)
as.list(match.call()[-1]) might help