2013-07-02 2 views
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 방법에 설정을 시도하지만 분명히 이것은 하나 갈 방법이 아니다 : 나는 당신이 첫번째 setOldclass를 사용하여 비 표준 클래스를 등록 할 필요가 있다고 생각

> 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) 

답변

8

.

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)