Let be:
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))
If we assign data.tableto the object slot and then change the external data table, the data table in the object slot will also be changed by reference:
p$prices = DT
p$prices
DT[a==1,b:=10]
p$prices
Do a similar experiment with xts:
p$prices = X
p$prices
X["1970-01-03"] <- 10
p$prices
Assigning an object xtsinside an object slot in this way seems to break the connection between the slot and the external object, unlike data.table.
How can one achieve that xtssharing a link?
source
share