6

浏览 Hadley Wickham 的 S4 wiki: https ://github.com/hadley/devtools/wiki/S4

setClass("Person", representation(name = "character", age = "numeric"), 
  prototype(name = NA_character_, age = NA_real_))
hadley <- new("Person", name = "Hadley")

我们如何为 Person 设计一个构造函数(像这样)

Person<-function(name=NA,age=NA){
 new("Person",name=name,age=age)
}

不这样做:

> Person()
Error in validObject(.Object) : 
  invalid class "Person" object: 1: invalid object for slot "name" in class "Person": got class "logical", should be or extend class "character"
invalid class "Person" object: 2: invalid object for slot "age" in class "Person": got class "logical", should be or extend class "numeric"
4

2 回答 2

4

看起来答案就在您的示例中:

Person<-function(name=NA_character_,age=NA_real_){
 new("Person",name=name,age=age)
}

产量

> Person()
An object of class "Person"
Slot "name":
[1] NA

Slot "age":
[1] NA

> Person("Moi")
An object of class "Person"
Slot "name":
[1] "Moi"

Slot "age":
[1] NA

> Person("Moi", 42)
An object of class "Person"
Slot "name":
[1] "Moi"

Slot "age":
[1] 42

但是,这完全不符合 S4 标准,并且重复了已在类定义中分配的默认值。也许你更愿意做

Person <- function(...) new("Person",...)

并牺牲没有命名参数的调用能力?

于 2011-10-21T17:11:57.817 回答
4

我更愿意给最终用户一些关于参数类型的提示,而不是...@themel 使用的建议。也放弃原型并使用length(x@name) == 0作为字段未初始化的指示,使用People类而不是Person,反映 R 的矢量化结构,并...在构造函数中使用,因此派生类也可以使用构造函数。

setClass("People",
    representation=representation(
        firstNames="character",
        ages="numeric"),
    validity=function(object) {
        if (length(object@firstNames) != length(object@ages))
            "'firstNames' and 'ages' must have same length"
        else TRUE
    })

People = function(firstNames=character(), ages=numeric(), ...)
    new("People", firstNames=firstNames, ages=ages, ...)

People(c("Me", "Myself", "I"), ages=c(NA_real_, 42, 12))
于 2011-10-21T18:34:17.560 回答