2

According to Hadley Wickham's Advanced R, an object with a single binding shouldn't get copied when it's modified (chapter 2.5.1). On my system, however, I can't see that modify-in-place optimisation is applied in this instance

x <- c(1,2,3)
tracemem(x)
#> "<0x10c3b16f8>"
x[[3]] <- 4
#> tracemem[0x10c3b16f8 -> 0x10b9305a8]:

That is, x is not supposed to be copied when it's modified by x[[3]] <- 4.

What am I getting wrong? Why doesn't R use modify-in-place optimisation in this example?

Edit: Added session info

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lobstr_0.0.0.9000

loaded via a namespace (and not attached):
[1] compiler_3.5.0   tools_3.5.0      yaml_2.2.0       Rcpp_1.0.0       rlang_0.3.0.9000
harisf
  • 149
  • 1
  • 1
  • 10
  • 1
    Related: https://stackoverflow.com/questions/51733925/copy-on-modify-what-happens-when-you-run-this-code-x-list110-x2 – markus Nov 11 '18 at 13:11
  • @JosephWood I'm glad this behaviour is reproducible at least. I just added my session info. – harisf Nov 11 '18 at 13:18
  • 1
    This post looks helpful: [What exactly is copy-on-modify semantics in R, ..](https://stackoverflow.com/q/15759117/4408538) – Joseph Wood Nov 11 '18 at 13:23
  • 3
    No copy is made in R GUI, so I suppose you run your code in RStudio. If so, this may be relevant: [Operator “[<-” in RStudio and R](https://stackoverflow.com/questions/15559387/operator-in-rstudio-and-r). "_Rstudio's object browser modifies objects it examines in a way that forces copying upon modification._".I _think_ this is mentioned somewhere in Hadley's book as well. – Henrik Nov 11 '18 at 13:27
  • @Henrik, I confirmed your remark. I can replicate the behavior in RStudio, but can't in R GUI. – Joseph Wood Nov 11 '18 at 13:31
  • @Henrik yes, you're right, I ran the above example in in RStudio. Upon running the example in terminal, I got the expected behaviour of modify-in-place optimisation. This is so interesting, thank you! – harisf Nov 11 '18 at 13:32
  • @Henrik, your comment seems to answer the question... maybe post as an answer? – Joseph Wood Nov 11 '18 at 13:34
  • 1
    My comment == the answer in the other post, so I feel no need to reiterate that answer here ;) – Henrik Nov 11 '18 at 13:38
  • 1
    I just flagged my question as a duplicative. I'm not entirely sure if that's the way to handle a duplicative that I've asked myself. Anyway, thanks for your help @Henrik and Joseph. – harisf Nov 11 '18 at 13:49
  • 2
    See also [In-place modification doesn't work in RStudio](https://community.rstudio.com/t/in-place-modification-doesnt-work-in-rstudio/9407) and links therein. – Henrik Nov 11 '18 at 13:52

0 Answers0