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
1
vote
1 answer

Getting a list of subclasses of a reference class

I created an AbstractFoo reference class and several subclasses, now I want to get all the names of the subclasses (AFoo, BFoo, CFoo). It seems this is possible using the subclasses slot of an S4 class, but I'd like to get the same sort of thing for…
bjb568
  • 11,089
  • 11
  • 50
  • 71
1
vote
0 answers

Validity test error when defining fields in a Reference Class

Context I've defined two RCs. The first is VectorCharacterElements. I've assigned a validity test to it. VectorCharacterElements <- setRefClass( "VectorCharacterElements", fields = list(vec = "character", el1 = "character", el2 =…
Joshua Daly
  • 606
  • 1
  • 7
  • 16
1
vote
1 answer

Possible to define indexing methods for R6/reference classes?

I have an R6 class that is basically a wrapper around a matrix-like object. I'd like to define a method for my class that lets me index and subset elements of the matrix directly. At the moment, my code looks something like this: cls <-…
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
1
vote
2 answers

slurm_apply a RefClass method from within a RefClass method

EDIT: New version of rslurm makes the solution very easy. See my answer below. Apologies for the somewhat longer than desired MWE, and a title that I realize after submitting the question may be needlessly complicated. I believe the real issue is…
rcorty
  • 1,140
  • 1
  • 10
  • 28
1
vote
0 answers

Reference class containing class "list" in R

I basically would like to create a new type of list with Reference Class. Instead of using S3 object, I also want to avoid static methods and define private functions within the reference class, such as: weirdList<-setRefClass("weirdList" …
chl111
  • 468
  • 3
  • 14
1
vote
1 answer

Customize the console printing for S4/RC objects in R

In R we can simply type the variable name in the console, the console will automatically print out the value. I have created a new S4/RC class define, and would like to create a nicer way to automatically "print" in the console. How do I edit the…
chl111
  • 468
  • 3
  • 14
1
vote
0 answers

How to implement a dispose pattern into a reference class in R?

I currently write a DBI compliant database interface. The DBI framework stipulates a method ´dbClearResult´, as : "Frees all resources (local and remote) associated with a result set". I have an R RC object containing metadata on the connection and…
Marcel Boldt
  • 252
  • 2
  • 12
1
vote
1 answer

Lists of reference class objects are not changing when the objects they were initialized with are changed

I have a list of reference class objects and I expect that when I change one of the objects the list was initialized with, the corresponding entry in the list should also change. This is not the case, as indicated in the following example code, if…
Gamund
  • 25
  • 2
1
vote
0 answers

R: Reference Class Field Assignment - numeric and NA

When assigning class types to fields in Reference Classes, what is the specification for numeric that allows for NAs? For example, if I use the following code and try to assign a value of NA to the field extinctTime, I receive an error…
Peter
  • 11
  • 2
1
vote
0 answers

Reference Classes in R multiple inheritance

I have 3 reference classes on R, let call them A,B,C. C inherits fields from both A and B but putting callSuper() in the initialize method of C this function call only A's initialize method and fields inherited from B are not initialized. How could…
Bmb58
  • 155
  • 2
  • 9
1
vote
1 answer

R allow reference classes to be member of reference class

I'm trying to make "class1" an element of "class2". But when "class1" has an initialize() function, I get the following error: class1 <- setRefClass( 'class1', fields = list(attribute1 = 'character'), methods = list( initialize =…
Robert Bray
  • 393
  • 3
  • 7
1
vote
1 answer

R Reference Class multiple inheritance: how to call method in a specific parent class?

I have a reference class Child which inherits from parents SuperA and SuperB. When in the initialize method of Child, I would like to invoke the initialize methods of both SuperA and SuperB in turn. Thus, for instance, I have: SuperA <-…
mchen
  • 9,808
  • 17
  • 72
  • 125
1
vote
1 answer

Call ReferenceClass methods in data.table's :=

I am having problems to call methods of ReferenceClass objects and assign the return values directly by reference to a data.table column (data.table version 1.9.3, R version 3.1) as this minimal example shows: RF <- setRefClass( Class = "RF", …
Beasterfield
  • 7,023
  • 2
  • 38
  • 47
1
vote
1 answer

Order of methods in R reference class and multiple files

There is one thing I really don't like about R reference class: the order you write the methods matters. Suppose your class goes like this: myclass = setRefClass("myclass", fields = list( x =…
qed
  • 22,298
  • 21
  • 125
  • 196
1
vote
1 answer

Output to pdf not working with ReferenceClasses methods in R?

Output to pdf not working with ReferenceClasses methods in R? This is an example taken from the ReferenceClasses R doc, with some minor modification: mEdit = setRefClass("mEdit", fields = list(data="matrix", edits="list")) mEdit$methods( edit…
qed
  • 22,298
  • 21
  • 125
  • 196