Hi,

while working with closures in deSolve we have found unexpected behavior
in R 3.1.0 and R 3.1.1 and R devel, while it was still as expected in R
3.0.3. This behavior occurs also in several functions of other packages
like nls.lm from minpack.lm and even nlm in base, while some other
functions worked as expected.  See example below.

The symptom is that super assignments (<<-) of unmodified variables lead
to "references" instead of copies.

Thomas Petzoldt and Karline Soetaert



## ----------------------------------------------------------
## Unexpected behavior:

# R version 3.1.0 (2014-04-10) -- "Spring Dance"
# Platform: x86_64-redhat-linux-gnu (64-bit)
#
## AND
#
# R version 3.1.1 (2014-07-10) -- "Sock it to Me"
# Platform: x86_64-w64-mingw32/x64 (64-bit)
#
## AND
#
# R Under development (unstable) (2014-08-31 r66504) --
# "Unsuffered Consequences"
# Platform: i386-w64-mingw32/i386 (32-bit)


f <- function(x, a) {
    cat(x[1], x.old[1], x.old1[1], x.old1 == x.old, "\n")
    x.old   <<- x          # 'reference'
    x.old1  <<- x * 1      # copy
    res <- sum((x-a)^2)
    attr(res, "gradient") <- 2*(x-a)
    res
}

x.old <- x.old1 <- 0
A <- nlm(f, c(10,10), a = c(3,5))

10 0 0 TRUE
10 10 10 TRUE TRUE
10.00001 10.00001 10 FALSE TRUE
10 10 10.00001 FALSE FALSE
-4 -4 10 FALSE FALSE
3 3 -4 FALSE FALSE


## ----------------------------------------------------------
## Expected behavior:
# R version 3.0.3 (2014-03-06) -- "Warm Puppy"
# Platform: x86_64-w64-mingw32/x64 (64-bit)


f <- function(x, a) {
    cat(x[1], x.old[1], x.old1[1], x.old1 == x.old, "\n")
    x.old   <<- x          # 'reference'
    x.old1  <<- x * 1      # copy
    res <- sum((x-a)^2)
    attr(res, "gradient") <- 2*(x-a)
    res
}

x.old <- x.old1 <- 0
A <- nlm(f, c(10,10), a = c(3,5))

10 0 0 TRUE
10 10 10 TRUE TRUE
10.00001 10 10 TRUE TRUE
10 10.00001 10.00001 TRUE TRUE
-4 10 10 TRUE TRUE
3 -4 -4 TRUE TRUE

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to