In-place modification doesn't work in RStudio

rstudio

#1

Take this code:

x <- runif(1e8)
pryr::refs(x)
system.time(x[[1]] <- 1)
system.time(x[[1]] <- 1)
  • If I run it in RStudio, all at once, modification is done in-place, OK.
  • If I run it in RStudio, one line at a time, it creates a copy when changing the first element of the vector.
  • If I run it in a terminal, it works fine.

Is it because of the object explorer of RStudio creating a reference to the same object so that it has to copy it if I modify one element?

Session info:

  • R 3.4.0
  • RStudio 1.1.442
  • Windows >= 10

#2

Is it because of the object explorer of RStudio creating a reference to the same object so that it has to copy it if I modify one element?

Probably!


But note the latest twist in the rather lengthy back and forth at the above link — it seems pretty hard to predict when you get a modify-in-place. I think the upshot for me was this bit of advice:

While determining that copies are being made is not hard, preventing such behaviour is. If you find yourself resorting to exotic tricks to avoid copies, it may be time to rewrite your function in C++, as described in Rcpp.