Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Variables with R6

Tags:

r

r6

I was curious if there was a way to create class variables for R6 classes within the definition of the class? I read through the Introduction to R6 classes vignette but didn't find any mention of class variables. I am able to create class variables after creating the class using ClassName$variableName <- initial_value but was curious if there was a way to do this within the actual class definition.

As an example, consider the Person class below, which has a class variable count which keeps track of the number of Person objects that have been instantiated:

library(R6)

Person <- R6Class("Person",
  public = list(
    name = NA,

    initialize = function(name) {
      Person$count <- Person$count + 1
      if (!missing(name)) self$name <- name
    }
  )
)

Person$count <- 0

Person$count # 0
john <- Person$new("John")
Person$count # 1
james <- Person$new("James")
Person$count # 2
like image 336
jeromefroe Avatar asked Sep 27 '22 12:09

jeromefroe


1 Answers

Not exactly what you were asking for, but this might help. You can add an environment to the class to store variables that are shared among all instances of a class. Because of the reference semantics of environments, this environment does not get reset whenever a new Person instance is created.

Here is an Example

library(R6)

Person = R6Class(
  "Person",
  public = list(
    name = NA,
    initialize = function( name ){
      if (!missing(name)) self$name <- name
      counter = self$getCounter()
      private$shared_env$counter = counter + 1
    },
    getCounter = function(){
      counter = private$shared_env$counter
      if( is.null( counter ) )
        counter =  0
      return( counter )
    }
  ),
  private = list(
    shared_env = new.env()
  )
)

Testing the code

john <- Person$new("John")
john$getCounter()
# 1
james <- Person$new("James")
james$getCounter()
# 2
john$getCounter()
# 2

You can combine this method with active bindings to make things like john$counter, etc work as well. If you want to decrease the counter when the object is destroyed, use the finalize method in R6.

like image 132
Gregor de Cillia Avatar answered Sep 30 '22 09:09

Gregor de Cillia