【发布时间】:2014-09-26 07:35:45
【问题描述】:
让我们来:
library(R6); library(data.table); library(xts)
Portfolio <- R6Class("Portfolio",
public = list(name="character",
prices = NA,
initialize = function(name, instruments) {
if (!missing(name)) self$name <- name
}
))
p = Portfolio$new("ABC")
DT = data.table(a=1:3, b=4:6)
X = xts(1:4, order.by=as.Date(1:4))
如果我们给对象槽赋值data.table,然后修改外部数据表,对象槽中的数据表也会被引用修改:
p$prices = DT
p$prices
DT[a==1,b:=10] # modify external table
p$prices # verify that the slot data is modified by reference
让我们用xts做一个类似的实验:
p$prices = X
p$prices
X["1970-01-03"] <- 10 # modify the external xts
p$prices # observe that modification didn't take place inside the object
与data.table 不同,以这种方式在对象槽内分配xts 对象似乎破坏了槽和外部对象之间的联系。
是否有可能实现 xts 通过引用共享?
【问题讨论】:
标签: r data.table xts r6