From thomas.petzoldt at tu-dresden.de  Tue Sep  2 09:57:33 2014
From: thomas.petzoldt at tu-dresden.de (Thomas Petzoldt)
Date: Tue, 02 Sep 2014 09:57:33 +0200
Subject: [Rd] unexpected behavior of <<- in nlm (lazy?)
In-Reply-To: <639722d585d84c4cae70c22574d15188@livia.nioz.nl>
References: <93c690838fdc4504a9e1a9dbc7fb054f@livia.nioz.nl>
	<54045BD9.20101@tu-dresden.de>
	<bec9aabc3bc841ad85877275ac2eaead@livia.nioz.nl>
	<54049F5E.60309@tu-dresden.de>
	<7c5958b7f5de4b8680a3f796bf42f724@livia.nioz.nl>
	<5404A3BB.3000107@tu-dresden.de>
	<639722d585d84c4cae70c22574d15188@livia.nioz.nl>
Message-ID: <5405786D.4020801@tu-dresden.de>

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



[Rd] unexpected behavior of <<- in nlm (lazy?)
Thomas Petzoldt