سؤال

Reference Classes only seem to accept the basic/standard object types are permitted. For instance, I want a chron object but this does not allow me to define it:

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

Is this a limitation or there is a better way? I tried maybe setting it in the initialize method but apparently this is not the way to go either:

> 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)
هل كانت مفيدة؟

المحلول

I think that you need to register your non standard class using setOldclass first.

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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top