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

Reference Class inheriting methods

I am re-factoring a package to use reference classes and running into an issue with the inheritance of methods. I have a class, B, that contains (inherits) A. However, I cannot call any of the methods for an instance of class 'B' that are inherited.…
Zelazny7
  • 39,946
  • 18
  • 70
  • 84
2
votes
1 answer

R: Fields in Reference Classes of type Reference Class

I have a reference class that I'd to use as an object inside another reference class. Example: class_1 <- setRefClass( Class = "class_1" , fields = list(nickname = "character", version = "character" ) , methods = list( initializer =…
ecksc
  • 148
  • 2
  • 9
2
votes
0 answers

What is the right way to lock a field for a R reference class within a package?

I'm trying to make a factory method for creating my reference class objects. I have one field which MUST be read-only, and so I want to give my package users a way to generate these reference class objects with a guaranteed lock on this field. My…
pan0ramic
  • 183
  • 9
2
votes
1 answer

R Reference Class: copy method with active bindings

It seems that the copy method does not work if you have an active binding. Example class: test <- setRefClass("test", fields = list(x =function() y + 1 , y = "numeric")) Initializing, it works ok: a <- test(y = 1) a$x [1] 2 a$y [1] 1 Error on…
Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66
2
votes
1 answer

Using a method/function within a reference class method of the same name

When defining a new reference class in R there is a bunch of boiler-plate methods that are expected (by R conventions), such as length, show etc. When these are defined they aggressively masks similar named methods/functions when called from within…
ThomasP85
  • 1,624
  • 2
  • 15
  • 26
2
votes
0 answers

roxygen2 ignores subclass constructor docstring

I am trying to document two reference classes with roxygen2: #' myclass #' @field fa A number #' @field fb A number #' @field filename A filename #' @field data A data frame Myclass = setRefClass("Myclass", fields = list( …
qed
  • 22,298
  • 21
  • 125
  • 196
2
votes
1 answer

How to extend a reference class defined in an R package that uses private functions?

Following on from a similar question, I would like to be able to allow users to extend a class in my package which uses private (not export-ed) functions. A minimal working example: # my_package/R/Main.R # My package: #' @import methods…
mchen
  • 9,808
  • 17
  • 72
  • 125
2
votes
1 answer

Save field of an R reference class

I am trying to save a field of an R reference class: myclass = setRefClass("myclass", fields = list( x = "numeric", y = "numeric" …
qed
  • 22,298
  • 21
  • 125
  • 196
2
votes
1 answer

How to extend a data.table using Reference Classes?

Problem: I wish to create a Reference Class that extends a data.table. The motivation being Want a data.table with custom methods and mutable fields Still want all existing syntax (such as indexing, subset, merge etc) to work as expected Problem…
mchen
  • 9,808
  • 17
  • 72
  • 125
2
votes
1 answer

Reference class inheritance from external package

I am trying to override a reference class method. Because reference class methods are bound to the class rather than the object, I believe in order to do this I need to define a new reference class which inherits from the old reference class. …
Jim
  • 4,687
  • 29
  • 30
2
votes
1 answer

Multiple acceptable classes in reference class field list

For example: Mycls = setRefClass( "Mycls", fields = list( # this is just a mock up colorvec = "numeric" | "factor" | "matrix" ) ) In this example, I want to allow colorvec to be numeric or factor or matrix. Is there a way to do…
qed
  • 22,298
  • 21
  • 125
  • 196
2
votes
1 answer

Documenting reference class with roxygen2 R CMD check codoc mismatches warning

I am trying to use roxygen2 to make documentation for a reference class object, but I keep getting this warning when I run R CMD check: S4 class codoc mismatches from documentation object 'myRefClass-class': Slots for class 'myRefClass' Code:…
samhiggins2001
  • 318
  • 2
  • 12
2
votes
1 answer

`[.` method for `ReferenceClass`

I would like to write a [. method for my ReferenceClass. So far, I have something like this: DT <- data.table(Index=1:5) MySeries <- setRefClass("MySeries", fields = list(data="data.table")) setMethod("[","MySeries",function(x, i,j,drop) { ii <-…
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
2
votes
1 answer

Manual modifications of the class definition of a Reference Class instance

I'm aware that this would be a terribly unreliable hack. But out of pure interest: What would you need to manually change in the .refClassDef field of an ref class object if the Reference Class definition of an already instantiated object changed…
Rappster
  • 12,762
  • 7
  • 71
  • 120
2
votes
1 answer

R Reference class and doparallel

Recently, I am trying to use reference class with parallel. I tried 4 different schemes, multicore, MPI, Socket and Forking. However, only multicore is able to produce correct result. and MPI, PSOCK and Forking all produce errors. PS: I was running…
Randy Lai
  • 3,084
  • 2
  • 22
  • 23