Hi all, I am bit of a beginner although I have dabbled in R before. Trying to learn the nitty gritty of R by following Advanced R, 2nd ed. (Wickham).
I was attempting example 4 in page 29. Please find the code below:
x <- list(1:10) x[] <- x
The explanation of the solution of this example can be found at https://advanced-r-solutions.rbind.io/names-and-values.html
However, I tried to use tracemem to explore how copy-on-modify works. So I added a few more lines in between, as shown below:
x <- list(1:10) tracemem(x) ref(x) x[] <- x ref(x)
The output I get is as follows:
x <- list(1:10)
tracemem[0x5600392d0218 -> 0x560037beec08]: FUN lapply ref
x[] <- x
tracemem[0x5600392d0218 -> 0x5600394c2598]:
tracemem[0x5600392d0218 -> 0x5600380a5988]: FUN lapply FUN lapply ref
From my understanding, the first list is at address <0x5600392d0218>. But when ref is called on x initially, there is a new address at 0x560037beec08. However, the initial "x" was not modified in any way, so what does this new address denote? I read somewhere in the book that it might be due to some sort of referencing done in the environment pane?
I am particularly confused, since in the very next line (i.e. the tree structure) shows the list being stored at mem addr 0x5600392d0218 (consistent with the initial address of the list) and the integer sequence at 0x56003918d610. So am I missing something?
After the following operation was carried out:
x[] <- x
tracemem indicates that a new list object has been created at address 0x5600394c2598. However, after calling ref(x), tracemem indicates that the new list was created at 0x5600380a5988? Again, I am a bit lost on what were held at 0x5600394c2598 and 0x5600380a5988.
I was further confused by the first line of the tree, which is showing the new list at 0x5600377d72e0. So whatever happened to the list objects at 0x5600394c2598 and 0x5600380a5988? To me, the third line which points to the address of the first list (at 0x5600392d0218]) and the mem address of the integer sequence (0x56003918d610), makes sense. However, what's going on in the middle?
Also, the output I received when I printed off x is as follows:
[]  1 2 3 4 5 6 7 8 9 10 [] [][]  1 2 3 4 5 6 7 8 9 10
I understand that to access elements of a list,  is needed. However, what does [][] mean (in the second line, after []?
I tried to output individual elements as follows and got slightly different results:
> x []  1 2 3 4 5 6 7 8 9 10 > x[]  1 2 3 4 5 6 7 8 9 10 > x [] [][]  1 2 3 4 5 6 7 8 9 10 > x[] []  1 2 3 4 5 6 7 8 9 10
I could not find any clear explanation of these differences. And compared to the output of x, these individual outputs are slightly different (in the indexing?). Any reason for that?
Sorry if my questions come off as naive, but I would grealty appreciate your help. Thanks!
Please see below for the version or R and Rstudio I am using:
system x86_64, linux-gnu
svn rev 74408
version.string R version 3.4.4 (2018-03-15)
nickname Someone to Lean On
P.s. Please find attached, the diagram of the explanation from the solutions website.