Torsten Hothorn
2004-Mar-29 10:22 UTC
[Rd] Global assignment with S4 objects in R 1.9.0 beta
Hi, some change during the last 10 days breaks code where slots of an object are changed globally: setClass("mylist", contains = "list") setClass("dummy", representation = representation( a = "mylist")) foo1 = function(i, x) { mydummy@a[[i]] <<- x ### change a slot } foo2 = function() { ### define an object mydummy <<- new("dummy") a <- vector(length = 10, mode = "list") class(a) <- "mylist" mydummy@a <<- a for (i in 1:10) foo1(i, i+1) mydummy } try(thisdummy <- foo2()) unlist(thisdummy@a) This one works as expected R : Copyright 2004, The R Foundation for Statistical Computing Version 1.9.0 alpha (2004-03-18), ISBN 3-900051-00-3 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R.> invisible(options(echo = TRUE)) > > setClass("mylist", contains = "list")[1] "mylist"> > setClass("dummy", representation = representation(+ a = "mylist")) [1] "dummy"> > foo1 = function(i, x) {+ mydummy@a[[i]] <<- x + }> > foo2 = function() {+ mydummy <<- new("dummy") + a <- vector(length = 10, mode = "list") + class(a) <- "mylist" + mydummy@a <<- a + for (i in 1:10) foo1(i, i+1) + mydummy + }> > try(thisdummy <- foo2()) > unlist(thisdummy@a)[1] 2 3 4 5 6 7 8 9 10 11> proc.time()[1] 1.99 0.06 2.03 0.00 0.00>and with yesterdays R-devel it fails R : Copyright 2004, The R Foundation for Statistical Computing Version 1.9.0 beta (2004-03-28), ISBN 3-900051-00-3 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. R> invisible(options(echo = TRUE)) R> R> setClass("mylist", contains = "list") [1] "mylist" R> R> setClass("dummy", representation = representation( + a = "mylist")) [1] "dummy" R> R> foo1 = function(i, x) { + mydummy@a[[i]] <<- x + } R> R> foo2 = function() { + mydummy <<- new("dummy") + a <- vector(length = 10, mode = "list") + class(a) <- "mylist" + mydummy@a <<- a + for (i in 1:10) foo1(i, i+1) + mydummy + } R> R> try(thisdummy <- foo2()) Error in foo1(i, i + 1) : Object "*tmp*" not found R> unlist(thisdummy@a) Error in unlist(thisdummy@a) : Object "thisdummy" not found Execution halted Best, Torsten