让我们:
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))
如果我们将 a 分配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
与. xts
_data.table
是否有可能以某种方式实现xts
通过引用共享?