Post edit: Important Remark: The behavior here reported seems to be happening in RStudio only, and not from the R terminal.
My RStudio version is: 1.2.1511.
I am trying to understand when an object in R is subject to changes in place or when is it following copy-on-modify
semantics.
Take this example from Hadley's Advanced R book.
In that example, Hadley is illustrating how an object in R can be modified in place. He talks of two cases: objects with single name binding and environments.
I tried his example with vector v
but I do not get the address of vector v
being preserved after changing one of its values.
After changing value 3
at position 3
of vector v
the memory address of v
changes from 0x5583a1461fb8
to 0x5583a2c5f608
.
So my question is why? This seems to contradict Hadley's book example.
v <- c(1, 2, 3)
pryr::address(v)
#> [1] "0x5583a1461fb8"
lobstr::obj_addr(v)
#> [1] "0x5583a1461fb8"
v[[3]] <- 4
pryr::address(v)
#> [1] "0x5583a2c5f608"
lobstr::obj_addr(v)
#> [1] "0x5583a2c5f608"
sessionInfo()
#> R version 3.5.1 (2018-07-02)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Arch Linux
#>
#> Matrix products: default
#> BLAS: /usr/lib/libblas.so.3.8.0
#> LAPACK: /usr/lib/liblapack.so.3.8.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_DK.utf8 LC_COLLATE=en_US.UTF-8
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.1 codetools_0.2-15 digest_0.6.18 rprojroot_1.3-2
#> [5] backports_1.1.2 magrittr_1.5 evaluate_0.12 rlang_0.3.4
#> [9] stringi_1.4.3 pryr_0.1.4 rmarkdown_1.10 lobstr_1.0.1
#> [13] tools_3.5.1 stringr_1.4.0 yaml_2.2.0 compiler_3.5.1
#> [17] htmltools_0.3.6 knitr_1.20
P.S. I've used the pryr
and lobstr
packages to find the memory addresses. I've also tried using tracemem()
but I got this error:
Error in tracemem(m) :
R was not compiled with support for memory profiling