From ripley at stats.ox.ac.uk  Mon Jan  5 12:05:21 2009
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 5 Jan 2009 11:05:21 +0000 (GMT)
Subject: [Rd] bug involving quote(); ghost in the machine
In-Reply-To: <alpine.LFD.2.00.0901051011100.25721@auk.stats.ox.ac.uk>
References: <94730b8a0901050204r6a8b97fcuac064ca1e339a91f@mail.gmail.com>
	<alpine.LFD.2.00.0901051011100.25721@auk.stats.ox.ac.uk>
Message-ID: <alpine.LFD.2.00.0901051101540.19474@auk.stats.ox.ac.uk>

On Mon, 5 Jan 2009, Prof Brian Ripley wrote:

> Turn off "keep.source": you will see that is foo that is changed by the call.
>
> Something is either not setting or handling the NAMED field  and so not 
> duplicating: it looks like the pairlist code for $<- is the culprit.

Yes, so try a current R-devel or R-patched.  Also, the [[<- version of 
your example works correctly (for me).

>
> On Mon, 5 Jan 2009, Felix Andrews wrote:
>
>> Hi list(...),
>> 
>> I've narrowed down a weird bug. It's like a ghost in the machine, in
>> that functions seem to remember things that they should not be able
>> to. In the example below, the result of the second (and subseqent)
>> calls depend on what was given in the first call.
>> 
>> foo <- function(given = NULL) {
>>    callObj <- quote(callFunc())
>>    if (!is.null(given))
>>        callObj$given <- given
>>    if (is.null(given))
>>        callObj$default <- TRUE
>>    callObj
>> }
>> 
>> foo()
>> # callFunc(default = TRUE)
>> foo(given = TRUE)
>> # callFunc(default = TRUE, given = TRUE)
>> 
>> Note, if the first call was something different, the result is different:
>> 
>> foo("blah blah")
>> # callFunc(given = "blah blah")
>> foo(given = TRUE)
>> # callFunc(given = TRUE)
>> foo()
>> # callFunc(given = "blah blah", default = TRUE)
>> 
>> So on subsequent calls, callObj is being initialised to its final
>> value from the first call. You can actually see this here:
>> body(foo)[[2]]
>> # callObj <- quote(callFunc(given = "blah blah"))
>> 
>> The problem seems to be related to quote(callFunc()), because if you
>> replace it with call("callFunc"), everything works as expected:
>> 
>> foo.ok <- function(given = NULL) {
>>    callObj <- call("callFunc")
>>    if (!is.null(given))
>>        callObj$given <- given
>>    if (is.null(given))
>>        callObj$default <- TRUE
>>    callObj
>> }
>> 
>> foo.ok()
>> # callFunc(default = TRUE)
>> foo.ok(given = TRUE)
>> # callFunc(given = TRUE)
>> 
>> 
>> 
>>> sessionInfo()
>> R version 2.9.0 Under development (unstable) (2009-01-04 r47462)
>> i386-pc-mingw32
>> 
>> locale:
>> LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia.1252;LC_MONETARY=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia.1252
>> 
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>> 
>> 
>> -- 
>> Felix Andrews / ???
>> http://www.neurofractal.org/felix/
>> 3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8
>> 
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> 
>
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/?ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/?ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] bug involving quote(); ghost in the machine
Prof Brian Ripley