So I'm writing a program in R using R6 (my bosses preference). It's got to do some heavy duty number crunching so I'm trying to get the key variables in the R6 classes to modify in place. Unfortunately what works for getting variables to modify in place in normal R doesn't seem to work inside an R6 class. I've constructed a minimal example below. You can clearly see variable inside the R6 class the variable jumps to a new memory address after the function. Outside the R6 class doing exactly the same thing causes no copy. Can any one give me any advice as to why and how I might get the variables in the class to modify in place?
my_r6 <- R6Class("my_r6",
public = list(
test = function() {
for (i in 1:5) {
private$x$a[i] <- 3
}
}
),
private = list(
x = list(a = c(1, 2, 3, 4, 5))
)
)
temp_r6 <- my_r6$new()
tracemem(temp_r6$.__enclos_env__$private$x$a)
temp_r6$test()
y <- list(b = c(1, 2, 3, 4, 5))
tracemem(y$b)
for (i in 1:5) {
y$b[i] <- 3
}