使用length()表示R中的新类

时间:2011-10-28 13:57:44

标签: class r

我正在尝试使用length()类上的函数item来显示向量options的长度。

setClass(Class = "item",
  representation = representation(
    options = "character"
  ),
  prototype = prototype(
    options = character()
  )
)
setGeneric("length")
setMethod("length", signature(x = "item"), definition = function(x) length(x@options))

但是,我总是收到以下错误:

Error: could not find function "getGeneric"
Warning:
In .rk.get.structure.global(".__C__item") :
  failure to get object .__C__item

我正在使用R version 2.13.2 (2011-09-30)和RKWard版本0.5.7z + 0.5.8 + devel1。

1 个答案:

答案 0 :(得分:1)

默认情况下,methods包在启动时不会加载。呼叫

library(methods)