Questions tagged [reference-class]

Reference classes are a new (as of R 2.12) way of object-oriented programming in R, such that objects are not copied but are instead mutable. Like most pass-by-reference methodologies, they are particularly well-suited to work with large datasets.

R has three object oriented (OO) systems: S3, S4 and Reference Classes (sometimes referred to as R5, yet their official name is Reference Classes).

Reference Classes were introduced in R 2.12. They fill a long standing need for mutable objects that had previously been filled by non-core packages like R.oo, proto and mutatr.

There are two main differences between reference classes and S3 and S4:

  • Refclass objects use message-passing OO
  • Refclass objects are mutable: the usual R copy on modify semantics do not apply

These properties makes this object system behave much more like Java and C#. The implementation of reference classes is almost entirely in R code, they are a combination of S4 methods and environments.

See also

156 questions
2
votes
1 answer

How to inherit from a class with mandatory argument in ctor?

It seems that if we create a class B that inherits from class A, A's constructor is called when B is being created. This leads to the following problem - A may have a mandatory parameter in its ctor (for use during instantiation), and so running the…
mchen
  • 9,808
  • 17
  • 72
  • 125
2
votes
0 answers

R reference classes - how to extend a generator object?

I want to extend a generator object, but all the R help files have on this subject is: Note that if one wanted to extend the reference class generator capability with a subclass, this should be done by subclassing refGeneratorSlot, not…
mchen
  • 9,808
  • 17
  • 72
  • 125
2
votes
1 answer

Calculated field with delayed evaluation in a `ReferenceClass`

In a stripped-down example, I have an object of Portfolio reference class holding individual asset values within the holdings field. There is additional field value, which is calculated by simply summing the individual values of holdings: Portfolio…
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
2
votes
1 answer

Why do I have to define methods of reference classes inside of reference classes?

Reference classes definitions can pile up quite some lines of code in R. When methods are defined inside a reference class a couple of methods plus the field definitions gives you a quite confusing class definition – at least it's hard to read at…
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
2
votes
1 answer

Set generic function for Reference Class

I get the folowing Error and I think it is because the interpreter thinks that I call the function .self$getFields. a<-A$new(var1=list(B$new(var1="a"),B$new(var1="b"))) a$test() [1] Error in getFields(l, c("var1")) : unused argument (c("var1")) …
Klaus
  • 1,946
  • 3
  • 19
  • 34
2
votes
2 answers

Call more then one slot or fields in S4 or Reference Classes

Is it possible to call or set values for more then one slot? A<-setClass(Class="A",slot=c(name="character",type="character")) a<-A() slot(object,c("name","type"),check=T) Do I have to write own getSlot and setSlot methods? And how to that in R5? AB…
Klaus
  • 1,946
  • 3
  • 19
  • 34
2
votes
1 answer

how to extend a reference class defined in an R package?

I want to allow users to extend a reference class I define in my package. Here is a toy example: # my_package/R/Main.R #' My Main class #' @export Main <- setRefClass("Main") After loading this package, I get a warning when I try to extend…
nachocab
  • 13,328
  • 21
  • 91
  • 149
2
votes
1 answer

Use a Reference Class as Levels of a Factor

Does anyone have any experience using Reference Classes as the levels of a factor? This is one step in my goal of adding "foreign-key-like" support to a data.frame in one of my packages. I'm ultimately looking to create a data.frame which can store,…
Jeff Allen
  • 17,277
  • 8
  • 49
  • 70
2
votes
1 answer

How to check the field assignment in Reference Classes

I have a question about the Reference Classes. How can I check the field assignments. Here my sample code: rm(list=ls(all=TRUE)) setRefClass( Class = "A", fields = list(firstValue = "numeric"), methods = list( initialize =…
exitia
  • 23
  • 3
2
votes
1 answer

r reference classes - do they have static field members/variables?

I have been playing a little with R's R5 class system to see what it can and can't do. In that process I have stumbled upon what looks like static class field members (which does not appear to be in the documentation - but i could have missed…
Mark Graph
  • 4,969
  • 6
  • 25
  • 37
2
votes
1 answer

Building an R package that defines reference classes

I am creating an R package. The package defines several reference classes. There is a dependency between the classes because one is the base class and the others are subclasses. Due to this dependency, the package files must be loaded in a certain…
pteetor
  • 511
  • 2
  • 12
1
vote
0 answers

R ReferenceClass: Call parent method in child object definition

I'm trying to implement object-specific method caching in R. I have many different kinds of objects, and many of them implement functions that get called often, take a long time to run, and whose inputs/outputs don't change between executions. So to…
Canadian_Marine
  • 479
  • 1
  • 4
  • 10
1
vote
1 answer

R Reference Class: Field of type 'units'

I have a reference class, one of the fields for which is a 'units' object from the R units package. My initial class definition looked like this: MyClass = setRefClass( "MyClass", fields = list(units_value = "numeric") ) uv…
Canadian_Marine
  • 479
  • 1
  • 4
  • 10
1
vote
0 answers

Is there a more efficient way to save instances of a reference class in R to RDS or RData files?

Consider the following example: Test <- setRefClass( "Test", fields = list(M = "matrix") ) x <- lapply(1:20000, function(i) matrix(rnorm(100), nrow = 10, ncol = 10)) y <- lapply(x, function(M) Test(M = M)) xts <- Sys.time() saveRDS(x,…
MikeKatz45
  • 545
  • 5
  • 16
1
vote
2 answers

how to get list of class names from reference class in R

I have this code: private$svg <- if(is(private$idaPlotObj, "DivosGridBmiPlot")){ ... } else { ... } in my code and I'm trying to refactor this code and get list of classes from private$idaPlotObj that is reference class, but all I get is…
jcubic
  • 61,973
  • 54
  • 229
  • 402