2016-09-15 5 views
3

모든 data.frame 메소드를 상속하는 클래스를 정의하는 데 문제가 있습니다. 내가 클래스 타임 라인을 정의했다 예를 들어, 이것을 data.frame로 표현 될 수있다 :data.frame 클래스를 data.frame 메소드를 상속하도록 R로 확장하십시오.

data.frame(task = c("Read Permutation City", "Learn S4 oop"), 
      from = c(Sys.Date(), Sys.Date()), 
      to = c(Sys.Date() + 5, Sys.Date() + 1)) 

대신 나는 data.frames 방법을 모두 유지할 것이다 클래스 "타임 라인"을 정의하고 싶지만 여러 방법 (예 : 줄거리, 요약 및 창)을 무시하고 추가하십시오. 다른 언어로도 이것은 매우 간단합니다.

S4 클래스 구조를 사용하여이 작업을 수행하고 싶습니다. contains 인수를 사용하여 S4 클래스를 구현하려고했지만, 결과가 data.frame에서 기대하는 바가 아니므로 뭔가 잘못하고 있어야합니다.

timeline <- setClass(Class = "timeline", contains = "data.frame") 

timeline <- function(task, from, to) { 
    new("timeline", data.frame(task = task, from = from, to = to)) 
} 

tm <- timeline("Run", Sys.Date(), Sys.Date() + 5) 
getClass("timeline") # Class "timeline" .... Extends: Class "data.frame", directly 
inherits(tm, "data.frame") # TRUE 

nrow(tm) # 0 
ncol(tm) # 3 

tm # Prints S4 info 
print(tm) # Expected output 

올바른 방법은 무엇입니까?

답변

0

setOldClass("data.frame")setClass() 앞에 사용하십시오.