3

引用类似乎只接受允许的基本/标准对象类型。例如,我想要一个 chron 对象,但这不允许我定义它:

> newclass <- setRefClass("newclass",fields=list(time="chron"))
Error in refClassInformation(Class, contains, fields, methods, where) : 
  class "chron" for field 'time' is not defined

这是一个限制还是有更好的方法?我尝试在initialize方法中设置它,但显然这也不是要走的路:

> newclass <- setRefClass("newclass",
+                         fields=list(time="numeric"),
+                         methods=list(initialize=function() time <<- as.chron(time)))
library(chron)
> x <- newclass(time=as.chron("2011-01-01"))
Error in .Object$initialize(...) : unused argument (time = 14975)
4

1 回答 1

9

我认为你需要先注册你的非标准类setOldclass

require(chron)
dts <- dates(c("05/20/13", "06/10/13"))
tms <- times(c("19:30:00", "22:30:05"))

setOldClass("chron")
newclass <- setRefClass("newclass",
                        fields = list(time = "chron"))

mydate <- newclass(time = chron(dates = dts, times = tms))
mydate$time
## [1] (05/20/13 19:30:00) (06/10/13 22:30:05)
于 2013-07-02T06:46:18.323 回答