[Rd] Reference Classes: strange behavior when trying to change class def in the same R session

Janko Thyson janko.thyson.rstuff at googlemail.com
Thu May 26 19:56:30 CEST 2011


Dear list,

I'm getting some strange behavior with Reference Classes every once in a 
while and I can't really put my finger on the cause. It'd be great if 
someone using Reference Classes could check if he/she experiences 
similar problems:

The thing is that when I alter the definition of a Reference Class in a 
running R session and source the respective file containing the class 
def, sometimes the changes are reflected and sometimes not.
I also tried to explicitly remove the class via 'removeClass()', and 
that too works at times and then again it doesn't. If it doesn't work it 
means that I have to shut down the session and start a new one, which is 
quite bothersome. I thought that it maybe had something to do with 
whether an explicit 'initialize' method has been defined or not, but the 
"bug" was still erratic.

Here's some code that you can test it with:

# S4 Classes
setClass(Class="Horst", representation=list(a="numeric"))
horst <- new("Horst", a=5)
horst
removeClass("Horst")
horst <- new("Horst")
setClass(Class="Horst", representation=list(a="character"))
horst <- new("Horst", a="blabla")
horst

# Reference Classes
setRefClass(Class="Horst", fields=list(a="numeric"), methods=list(
         foo=function() print("hello world!")))
setMethod(
     f="initialize",
     signature=signature(.Object="Horst"),
     definition=function(
         .Object
     ){
         return(.Object)
     }
)
gen <- getRefClass("Horst")
horst <- gen$new()
horst$a
horst$a <- 5
horst$a
horst$foo()

removeClass("Horst")

setRefClass(Class="Horst", fields=list(a="character"), methods=list(
         foo=function(x) print(x)))
gen <- getRefClass("Horst")
horst <- gen$new()
horst$a
horst$a <- 5
horst$a
horst$foo(x=5)

Most of the times the change from the first to the second config of ref 
class "Horst" produced a problem and required a 'removeClass()'. 
Strangely, going from the second config back to the first usually works 
without 'removeClass()'.

Thanks in advance for taking the time to play this through,
Janko

R version 2.13.0 (2011-04-13)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252

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

loaded via a namespace (and not attached):
[1] codetools_0.2-8 tools_2.13.0



More information about the R-devel mailing list