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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With