Define class methods and class variables in R5 reference class

I want to know the correct way to define class methods and a class variable in an R5 reference class.

Here is an example:

> # define R5 class XX
> # member variable: ma
> # member method: mfa
> XX <- setRefClass("XX",
+   fields = list(ma = "character"),
+   methods = list(
+     mfa = function() return(paste(ma, "*"))
+     ))
> 
> XX
Generator object for class "XX":

Class fields:

Name:         ma
Class: character

 Class Methods:  
    "callSuper", "copy", "export", "field", "getClass", "getRefClass", "import", "initFields", 
"mfa"


 Reference Superclasses:  
    "envRefClass"

> # create an instance of XX
> x <- XX$new(ma="ma")
> 
> # call member method refering to the member variable.
> x$mfa()
[1] "ma *"
> 
> # here, I define *class* variable
> XX$cc <- "cc"

> # contents of XX
> ls(XX)
[1] "cc"        "className" "def"       "methods"   "new"      

> # here, I define member method referring to the class var.
> XX$methods(mfc = function() {
+   return(XX$cc)
+ })

> # it does work.
> x$mfc()
[1] "cc"

XX$cc <- "cc"behaves as if it ccis a variable of class XX, but I'm not sure if this is the right way.
For example, XX $ def <- "hoge" can break a class XX generator.

So, I want to know if there is a standard way to define class variables and methods.

Thanks in advance.

+5
source share
1 answer

"" , . - R, , - OO

+6

All Articles