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
3
votes
2 answers

Generic functions and reference classes in R

I am reading Martin Morgan's notes on reference classes, and on slide 7, he uses setMethod to define the show function without setting it previously as a generic using setGeneric. Why is this allowed? Is there a list of methods that are declared as…
tchakravarty
  • 10,736
  • 12
  • 72
  • 116
3
votes
1 answer

How to debug methods from Reference Classes?

How to debug a call like getFields? I tried library(debug); mtrace(AB.setFields) but nothing happend. Besides there are some better ways to define AB.setFields? AB.getFields<-function(){ return(list(name,var2)) } AB.setFields<-function(fields){ …
Klaus
  • 1,946
  • 3
  • 19
  • 34
3
votes
1 answer

How to avoid prepending .self when using eval in a reference class in R?

I need to use eval to call a reference class method. Below is a toy example: MyClass <- setRefClass("MyClass", fields = c("my_field"), methods = list( initialize = function(){ my_field <<- 3 }, hello =…
nachocab
  • 13,328
  • 21
  • 91
  • 149
3
votes
1 answer

defining non-standard classes in Reference Class object

Reference Classes only seem to accept the basic/standard object types are permitted. For instance, I want a chron object but this does not allow me to define it: > newclass <- setRefClass("newclass",fields=list(time="chron")) Error in…
hatmatrix
  • 42,883
  • 45
  • 137
  • 231
3
votes
1 answer

How to set a field in the parent class in R using reference classes?

I'm trying to set the params field and validate it in the Template class, so I can do TemplateClass$new(params) and it automatically validates, but I get an error: Template <- setRefClass('Template', fields = c( "params" ), …
nachocab
  • 13,328
  • 21
  • 91
  • 149
3
votes
1 answer

R setRefClass accessors

I have created a setRefClass, I would like to know how you can implement the accessors so that when you create a new instance of this class you can access the fields by using setXXX, getXXX. I was thinking of using…
pam
  • 676
  • 1
  • 7
  • 27
3
votes
1 answer

Passing reference to Reference Class object between two other Reference Class objects (soccer example)

I am trying to "pass" a reference to an Reference Class object (say, a ball) between two other Reference Class objects (say, two soccer [football] players) with the following example: # create Reference classes b <- setRefClass('Ball', fields =…
Gary Weissman
  • 3,557
  • 1
  • 18
  • 23
3
votes
2 answers

Instantiation of reference classes within reference classes - problems with lock() and immutability

I have come across some behaviour from R reference classes I would like to work around. In the following code, reference class B has two fields of reference class A in it. These fields in B appear to be instantiated (possibly twice) with a…
Mark Graph
  • 4,969
  • 6
  • 25
  • 37
3
votes
1 answer

Reference class fields disappearing

I decided to give Reference Classes another shot, but my first hello world is already giving me issues. What is going wrong here? > memory <- setRefClass( + Class = "memory", + fields = list(state="vector"), + methods = list( + get =…
Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
2
votes
1 answer

list (or array) of RefClass object in Reference Class

I can make a parent class containing an object setRefClass("img",fields=list(name="character")) setRefClass("imgs",fields=list(img="img")) However, I want to create an RefClass object with an array of other RefClass objects. I can use a "list" as…
Sang
  • 535
  • 5
  • 14
2
votes
1 answer

Evaluation nested too deeply when using reference class with active binding

Reproducible example: bed2_RC <- methods::setRefClass( "bed2", fields = list( .fam = "data.frame", #### Active bindings fam = function() { if (ncol(.self$.fam) == 0) { .self$.fam <- datasets::iris } …
F. Privé
  • 11,423
  • 2
  • 27
  • 78
2
votes
1 answer

R Setting field with same type as reference class type?

I'm trying to create a class in R called move, and want one of the fields to be type move as well. I know this is possible in Java, but I'm unsure how to do this in R (if it can be done). I've tried searching for examples, but have been unable to…
aklobo
  • 21
  • 3
2
votes
1 answer

How to speed up writing to a matrix in a reference class in R

Here is a piece of R code that writes to each element of a matrix in a reference class. It runs incredibly slowly, and I’m wondering if I’ve missed a simple trick that will speed this up. nx = 2000 ny = 10 ref_matrix <- setRefClass( …
KeithS
  • 113
  • 1
  • 7
2
votes
1 answer

Extending as.data.frame and as.matrix methods for an object created via setRefClass

I'm creating a reference class object in a manner similar to the example below: # Class ------------------------------------------------------------------- myDataFrame <- setRefClass(Class = "myDataFrame", fields =…
Konrad
  • 17,740
  • 16
  • 106
  • 167
2
votes
1 answer

How to use ellipsis to pass arguments and evaluate in another environment in R

I am trying to pass quite a lot of arguments to a function (actually it's a reference class initialize function). I am thinking about using three dots ellipsis to pass arguments to the class initializer, but it doesn't work. Here is my sample…
chl111
  • 468
  • 3
  • 14