In-place modification doesn't work in RStudio

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

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.

2 Likes

I actually was just about to post something on these forums asking the same thing. Mainly because I was reading Advanced R and ran into the following lines:

(Note that if you’re using RStudio, refs() will always return 2: the environment browser makes a reference to every object you create on the command line.)

and

When refs(x) is 1, modification will occur in place. When refs(x) is 2, R will make a copy (this ensures that other pointers to the object remain unaffected).

http://adv-r.had.co.nz/memory.html#modification

So it seems like you'll always make a copy when doing things in RStudio, but it's interesting that you found that running all at once in RStudio will do modification in-place. I wonder why that is / why it would matter? Does RStudio no make its environment references until all the lines queued to be run are completed?

RStudio updates the environment pane after the last set of R code input by the user has finished executing. One place where you might see a difference is if you were to source an R script from RStudio vs. running it line-by-line (since running line-by-line would give the Environment pane a chance to update in-between each line executed)

1 Like