Bug 15098 - Subassignment to a list component may affect others
Subassignment to a list component may affect others
Status: RESOLVED FIXED
Product: R
Classification: Unclassified
Component: Language
R 2.15.1 patched
All All
: P5 normal
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-11-07 11:53 UTC by Suharto Anggono
Modified: 2012-12-17 21:03 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Suharto Anggono 2012-11-07 11:53:45 UTC
Here is an illustration of the problem.

> x <- list()
> x[1:2] <- list(1)
> x
[[1]]
[1] 1

[[2]]
[1] 1

> x[[1]][] <- 2   # change part of first component of x
> x   # second component of x should not be affected, but is affected
[[1]]
[1] 2

[[2]]
[1] 2


The following is OK.

> x <- list()
> 1 -> x[[1]] -> x[[2]]
> x
[[1]]
[1] 1

[[2]]
[1] 1

> x[[1]][] <- 2
> x
[[1]]
[1] 2

[[2]]
[1] 1
 

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base


The earliest R version that I have is 2.5.1. It is in the same computer. I see the same behavior there.
Comment 1 Peter Dalgaard 2012-11-08 10:10:14 UTC
Same thing confirmed on OSX, most likely platform independent. 

Something forgot to bump NAMED, I guess.
Comment 2 Luke Tierney 2012-11-14 15:45:34 UTC
Incremented NAMED on RHS values when used more than once.
Comment 3 Suharto Anggono 2012-11-19 11:31:55 UTC
These are additional similar examples that I found.

> ## using '$'
> x <- list()
> x[c("a","b")] <- list(1)
> x
$a
[1] 1

$b
[1] 1

> x$a[] <- 2
> x
$a
[1] 2

$b
[1] 2

>
> ## originally assign list component one by one (no recycling)
> x <- list()
> list(1) -> x[1] -> x[2]
> x
[[1]]
[1] 1

[[2]]
[1] 1

> x[[1]][] <- 2
> x
[[1]]
[1] 2

[[2]]
[1] 2

>
> ## give list component from a variable
> y <- list(1)
> x <- list()
> x[1] <- y
> x
[[1]]
[1] 1

> y
[[1]]
[1] 1

> x[[1]][] <- 2
> x
[[1]]
[1] 2

> y  # also changed
[[1]]
[1] 2

>
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
Comment 4 Martin Maechler 2012-11-19 12:30:44 UTC
(In reply to comment #3)

Well, but did you not notice ("Comment #2") that the bug has been FIXED
in the mean time?
(in R 2.15.2 patched and R devel).

The new example works as well as your previous one since bug fix.
Comment 5 Suharto Anggono 2012-11-20 05:57:58 UTC
(In reply to comment #4)
> (In reply to comment #3)
> 
> Well, but did you not notice ("Comment #2") that the bug has been FIXED
> in the mean time?
> (in R 2.15.2 patched and R devel).
> 
> The new example works as well as your previous one since bug fix.

So, just now, I tried R-devel, binary build for Windows. Here is the result for my last example.

> y <- list(1)
> x <- list()
> x[1] <- y
> x
[[1]]
[1] 1

> y
[[1]]
[1] 1

> x[[1]][] <- 2
> x
[[1]]
[1] 2

> y   # should not be changed, but is changed
[[1]]
[1] 2

> sessionInfo()
R Under development (unstable) (2012-11-18 r61128)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
Comment 6 Martin Maechler 2012-11-20 09:08:02 UTC
At Comment #5:  You are right; that one is not yet fixed (I overlooked the fact that you gave three new examples) --> Reopened bug report
Comment 7 Luke Tierney 2012-12-17 21:03:25 UTC
Set NAMED to 2 when RHS container or content has NAMED > 1. Fixed in r61364 in trunk and r61365 in R-patched.

It would have been better to make this a separate bug report as it is a different issue than the original one.