[Question changed after answers]
Thanks for answers. In my question, I was unclear, for which I apologize.
I will try to give more detailed information about our situation. We have c. 100 that we save in the environment. Each of them is very large. If at all possible, we want to avoid copying these matrices when performing updates. We often encounter a memory limit of 2 GB, so this is very important for us.
So, our two requirements: 1) avoid copies and 2) indirectly access matrices by name. Speed, although important, is a side issue that could be addressed by avoiding copying.
It seems to me that Tommy's solution included making a copy (although it fully answered my actual original question, so I'm wrong).
Below is the code that seems most obvious to us, but it clearly creates a copy (as shown by the increase in memory.size)
myenv <- new.env() myenv$testmat1 <- matrix(1.0, nrow=6000, ncol=200) testfnDirect <- function(paramEnv) { print(memory.size()) for (i in 1:300) { temp <- paramEnv$testmat1[10,] paramEnv$testmat1[10,] <- temp * 0 } print(memory.size()) } system.time(testfnDirect(myenv))
Using the keyword c seems to avoid this, as shown below:
myenv <- new.env() myenv$testmat1 <- matrix(1.0, nrow=6000, ncol=200) testfnDirect <- function(paramEnv) { print(gc()) varname <- "testmat1"
However, this code works by accessing testmat1 directly by name. Our problem is that we need to indirectly touch on this issue (we do not know in advance which matrices we will update).
Is there a way to change testfnDirect so that we use the varname variable rather than hardcoding testmat