From h.wickham at gmail.com  Thu Jul 25 14:42:26 2013
From: h.wickham at gmail.com (Hadley Wickham)
Date: Thu, 25 Jul 2013 07:42:26 -0500
Subject: [Rd] Understanding modification in place
In-Reply-To: <59139924-1571-4904-81C6-9486DC964347@r-project.org>
References: <CABdHhvGCcVdGFPwEW9yxG8UBQJXj2x+rEFEgTTkBcqLSs_M6=Q@mail.gmail.com>
	<59139924-1571-4904-81C6-9486DC964347@r-project.org>
Message-ID: <CABdHhvFY6JhtmMGqyScPjz2d6Ag=GcvJLXCFy2duwAZhjkAAsQ@mail.gmail.com>

>> If I run it from the command line, I get:
>>
>> Global
>> @1050bb840 13 INTSXP g0c2 [MARK,NAM(1)] (len=3, tl=0) 1,2,3
>> @1050bb840 13 INTSXP g0c2 [MARK,NAM(1)] (len=3, tl=0) 1,1,3
>> In function
>> @1050bb190 13 INTSXP g0c2 [NAM(1)] (len=3, tl=0) 1,2,3
>> @1050bb190 13 INTSXP g0c2 [NAM(1)] (len=3, tl=0) 1,1,3
>>
>> i.e. both modifications occur in place.
>>
>> If I put it in a file and source() it, I get:
>>
>> Global
>> @1050bb318 13 INTSXP g0c2 [NAM(2)] (len=3, tl=0) 1,2,3
>> @1050bb698 13 INTSXP g0c2 [NAM(1)] (len=3, tl=0) 1,1,3
>> In function
>> @1050b8958 13 INTSXP g0c2 [NAM(1)] (len=3, tl=0) 1,2,3
>> @1050b8958 13 INTSXP g0c2 [NAM(1)] (len=3, tl=0) 1,1,3
>>
>> i.e. in the global environment a copy is created.
>>
>> Why is there a difference?
>>
>
> Did you look at source()? ;)
> It has to do with all the additional processing there which assigns the result of every line locally thus producing extra copies. If you just eval it, you'll get the same behavior as in the console:

Oh, of course.  Thanks!

Hadley

-- 
Chief Scientist, RStudio
http://had.co.nz/



[Rd] Understanding modification in place
Hadley Wickham