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

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jan 5 11:25:29 CET 2009


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.

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


More information about the R-devel mailing list