See this short example:
library(R6)
library(pryr)
Person <- R6Class("Person", public = list(name = NA, hair = NA, initialize = function(name,
hair) {
if (!missing(name)) self$name <- name
if (!missing(hair)) self$hair <- hair
}, set_hair = function(val) {
self$hair <- val
}))
ann <- Person$new("Ann", "black")
address(ann)
#> [1] "0x27e01f0"
ann$name <- "NewName"
address(ann)
#> [1] "0x27e01f0"
ann2 <- Person$new("Ann", "white")
g <- c(ann, ann2)
address(g)
#> [1] "0x32cc2d0"
g[[1]]$hair <- "red"
address(g)
#> [1] "0x34459b8"
I was expecting the operation g[[1]]$hair <- "red"
will change g
by reference like ann$name <- "NewName"
. Is there a way to achieve this?