使用“initialize”方法和slots属性时setClass错误

时间:2017-02-14 01:40:00

标签: r

基于setClass文档,不推荐使用参数representation,建议使用特殊方法:“initialize”。我收到以下错误:

   Error in setClass("Person", slots = c(name = "character", address = "character",  : 
  Argument "representation" cannot be used if argument "slots" is supplied

对于这个简单的课程:

setClass("Person",
     slots =c(name = "character", address = "character", phone = "character"),
     setMethod("initialize",
       "Person",
       definition=function(.Object, aName = character(0), aAddress = character(0), 
         aPhone=character(0)) {
           .Object@name <- aNname
           .Object@address <- aAddress
           .Object@phone <- aPhone
           .Object
       }
     )
)

尝试了其他选项,但我总是遇到错误。我肯定错过了一些东西。

编辑(基于@Sathish解决方案)

如何定义其他方法?,例如setName

setClass(Class = "Person",
    slots = representation(name = "character", address = "character", phone = "character"),
    setMethod(f = "setName", signature = c("character"),
        definition = function(.Object, aName) {
        .Object@name <- aName
    })
)

我知道它必须在类定义中定义,因为在setMethod中我没有看到如何将方法与类相关联,signature根据setMethod文档,用于参数定义。我犯了同样的错误。

  1. 这是使用对象本身的正确方法(在其他语言中使用特殊this表示法,这里似乎是.Object符号)?。也许我来自java-mind集,我不知道在R.下的OO的想法

  2. 定义访问方法是一个好习惯,例如:get / set,或者只使用@ - 语法?person@name <- aName for { {1}}方法或set for get方法,其中person@name是类person的实例?

1 个答案:

答案 0 :(得分:1)

crepresentation都做同样的事情,即为类表示创建命名向量。

representation()是一个实用函数,用于创建带有值作为数据类型的命名向量。它用于向后兼容representation参数

c()可用于为slots创建命名向量,但不能用于representation参数。但是使用list()将适用于slotsrepresentation个参数。见下面的例子。

# create a class 'Person'
# with slots argument and representation() function
setClass(Class = "Person",
         slots = representation( name = "character", address = "character", phone = "character"))
getClass('Person')
removeClass('Person')

# with slots argument and c() function
setClass(Class = "Person",
         slots = c( name = "character", address = "character", phone = "character"))
getClass('Person')
removeClass('Person')

# with representation argument and representation() function
setClass(Class = "Person",
         representation = representation( name = "character", address = "character", phone = "character"))
getClass('Person')
removeClass('Person')

# with representation argument and c() function
setClass(Class = "Person",
         representation = c( name = "character", address = "character", phone = "character"))
# Error in validObject(.Object) : 
#   invalid class “classRepresentation” object: invalid object for slot "slots" in class "classRepresentation": got class "character", should be or extend class "list"

getClass('Person')
# Error in getClass("Person") : “Person” is not a defined class

# with list()
setClass(Class = "Person",
         representation = list( name = "character", address = "character", phone = "character") )
getClass('Person')
removeClass('Person')

setClass(Class = "Person",
         slots = list( name = "character", address = "character", phone = "character") )
getClass('Person')
removeClass('Person')

# initialize method during object instantiation
setMethod(f = "initialize", signature = "Person",
          definition = function( .Object, Aname, Aaddress, Aphone) 
          { 
            .Object@name    <- Aname
            .Object@address <- Aaddress
            .Object@phone   <- Aphone
            return( .Object )
          } )

通过调用setMethodremoveMethod(s)

来声明和分配或删除通用函数的方法
# getters and setters
# reserve the name of the method by using setGeneric() and standardGeneric()
setGeneric( name = "getName", signature = 'obj', def  = function( obj ) standardGeneric( "getName" ))       # "getName"
setGeneric( name = "setName", signature = 'obj', def  = function( obj, name ) standardGeneric( "setName" )) # "setName"

# set the reserved method getName | setName to Person Class
setMethod( f = "getName", signature = "Person", definition = function( obj ) return( obj@name ))                         # "getName"
setMethod( f = "setName", signature = "Person", definition = function( obj, name ) { obj@name <- name; return( obj ) } ) # "setName"

showMethods('getName')
# Function: getName (package .GlobalEnv)
# obj="Person"

showMethods('setName')
# Function: setName (package .GlobalEnv)
# obj="Person"

# create new instance of the class 'Person' using new()
myobj <- new(Class = "Person", Aname = "bob", Aaddress = "xxxx", Aphone = "243-344-3434")

myobj
# An object of class "Person"
# Slot "name":
#   [1] "bob"
# 
# Slot "address":
#   [1] "xxxx"
# 
# Slot "phone":
#   [1] "243-344-3434"

getName( myobj )
# [1] "bob"

myobj <- setName( myobj, 'bill')
getName( myobj )
# [1] "bill"

清理课程和方法

removeMethod('getName', signature = 'Person')
# [1] TRUE
removeMethod('setName', signature = 'Person')
# [1] TRUE
removeClass('Person')
# [1] TRUE
removeGeneric('getName')
# [1] TRUE
removeGeneric('setName')
# [1] TRUE