Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define class methods and class variables in R5 reference class

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.

like image 746
kohske Avatar asked Apr 22 '11 06:04

kohske


People also ask

What are class methods in Java?

Class methods are methods that are called on the class itself, not on a specific object instance. The static modifier ensures implementation is the same across all class instances. Many standard built-in classes in Java (for example, Math) come with static methods (for example, Math.

What is a class variable in Ruby?

Used declare variables within a class. There are two main types: class variables, which have the same value across all class instances (i.e. static variables), and instance variables, which have different values for each object instance.

What are class methods in Python?

A class method is a method which is bound to the class and not the object of the class. They have the access to the state of the class as it takes a class parameter that points to the class and not the object instance. It can modify a class state that would apply across all the instances of the class.

What are methods in R?

Methods are frequently defined for functions that are non-generic in their original package,. for example, for function plot() in package graphics. An identical version of the corresponding generic function may exist in several packages. All methods will be dispatched consistently from the R session.


1 Answers

Regarding a "standard" for class variables, no such standard exists. Reference classes are a new feature to R, and to the extent that some aspect of OO isn't documented in the official documentation on reference classes, it's safe to assume that it hasn't yet been standardized.

As for whether such a standard might appear, I doubt it. Class variables are really just volatile globals in a pseudo-namespace, and relying on external state generally isn't the R way of doing things. The existence of reference classes is already a pretty big concession against this to improve performance.

But, as you noted, what you've done does work. R is a language for consenting adults, and it won't try to stop you from introducing messiness at your own risk.

like image 145
goodside Avatar answered Sep 30 '22 02:09

goodside