From luke-tier@ey m@ili@g off uiow@@edu  Sun May 13 22:44:37 2018
From: luke-tier@ey m@ili@g off uiow@@edu (luke-tier@ey m@ili@g off uiow@@edu)
Date: Sun, 13 May 2018 15:44:37 -0500 (CDT)
Subject: [Rd] 
 different type of modification when code is sourced and pasted
In-Reply-To: <CADP6=aWqPx-gSYnuYCqVKfxaVrY43nAW_oZP7hP-FZiFhz8gkQ@mail.gmail.com>
References: <CADP6=aWqPx-gSYnuYCqVKfxaVrY43nAW_oZP7hP-FZiFhz8gkQ@mail.gmail.com>
Message-ID: <alpine.DEB.2.20.1805131541330.6923@luke-Latitude-7480>

Basically as a result of all the bells and whistles source() supports
it ends up creating an additional reference to the value of each top
level expression. That forces a duplicate.

Best,

luke

On Sun, 13 May 2018, Krzysztof Bartoszek wrote:

> Dear all,
> I have a question about R's modification in place and replacement functions.
> I was working through Hadley Wickham's Advanced R section on
> Replacement Functions and
> in a file (say test.R) I wrote:
>
> `second<-` <- function(x, value) {
>   x[2] <- value
>   x
> }
> x <- 1:10
> second(x) <- 5L
> print(x)
>
> library(pryr)
> x <- 1:10
> print(address(x))
> second(x) <- 6L
> print(address(x))
> x <- 1:10
> print(address(x))
> x[2] <- 7L
> print(address(x))
>
> When I copy-paste the code the effect is as expected:
>> `second<-` <- function(x, value) {
> +    x[2] <- value
> +    x
> + }
>> x <- 1:10
>> second(x) <- 5L
>> print(x)
> [1]  1  5  3  4  5  6  7  8  9 10
>>
>> library(pryr)
>> x <- 1:10
>> print(address(x))
> [1] "0x1b44ef40"
>> second(x) <- 6L
>> print(address(x))
> [1] "0x1f955a38"
>> x <- 1:10
>> print(address(x))
> [1] "0x1b447090"
>> x[2] <- 7L
>> print(address(x))
> [1] "0x1b447090"
>
> However, when I source the code the effect is a bit surprising:
>> source("test.R")
> [1]  1  5  3  4  5  6  7  8  9 10
> [1] "0x2f372320"
> [1] "0x2f394888"
> [1] "0x1b446ef0"
> [1] "0x1f960f30"
> Notice, that the last two addresses are different.
> I would be grateful if somebody could point me to an explanation for this.
> I have tried this on: R version 3.1.2 running on openSUSE 12.1, R
> version 3.4.2 running on openSUSE Leap 42.3
> and on R running on a friend's Mac.
>
> Thank you
> Best wishes
> Krzysztof Bartoszek
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



[Rd] different type of modification when code is sourced and pasted
luke-tier@ey m@ili@g off uiow@@edu