I want to know the correct way to define the class methods and class variable in 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"
The XX$cc <- "cc"
behaves as if the cc
is class variable of XX, but I'm not sure if this is a correct way.
For example, XX$def <- "hoge" can break the XX class generator.
So, I want to know if there is a standard way to define class variable and methods.
Thanks in advance.