I have a function in R that should modify a list by appending new items to it. However I really do not want to make a copy of my list.
I know this is not an easy task in R, and I have seen some minimal examples using the eval.parent function, but I do not know how they would apply to my particular case.
A minimal example of my problem (not my actual function) would look like
L <- list(o1 = 1, o2 = 2, o3 = 3)
add_to_list <- function(L){
n1 <- sum(unlist(L))
n2 <- mean(unlist(L))
L$n1 <- n1
L$n2 <- n2
return(L)
}
L <- add_to_list(L)
If I'm correct, as L is modified, the function add_to_list will make a full copy of L, including o1, o2, and o3, in this example? (It appears so when I look at the computation time of my actual function.)
I would like to pass L by reference as this would drastically increase the performance of my code. In my real example, there are more than three objects o1, o2, and o3, and some of them are really large, and I also have a lot of code before the final assignments which is why I want it modularized as function.