Search code examples
rfieldxtszooreference-class

In R reference class, how to define fields as "xts" objects


I am defining a reference class as follow:

test = setRefClass(
  Class = "test",
  fields = c(
    edata = "data.frame"
  )
)
test$methods(
  getdata = function(newdata,...){
    edata <<- newdata
  }
)

And then I use the following code:

test1 = test$new()
x = xts(data.frame(val=1, val2=2), order.by=as.Date("2015-01-02"))
test1$getdata(x)

there's an error message that "cannot assign data.frame" class by "zoo" I then modified the code:

test = setRefClass(
  Class = "test",
  fields = c(
    edata = "zoo"
  )
)

But, after library(zoo), or library(xts), I will get a following message:

 Error in as(value, fieldClass, strict = FALSE) : 
  internal problem in as(): “xts” is(object, "zoo") is TRUE, but the metadata asserts that the 'is' relation is FALSE 

I really need to use the edata as an "xts" object in this class, because I would use so many functions that have already written for xts!

is there any way to deal with this problem?


Solution

  • One can specify the class as "ANY"

    test = setRefClass(
      Class = "test",
      fields = c(
        edata = "ANY"
      )
    )
    

    Then one can assign an "xts" object to "edata".