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

Felix Andrews felix at nfrac.org
Mon Jan 5 11:04:40 CET 2009


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



More information about the R-devel mailing list