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
4
votes
1 answer

Inconsistency of S4 dispatch behavior for R6 classes

Actual questions Shouldn't the fact that R6 classes inherit from (informal S3) class R6 allow the definition of S4 methods for signature arguments of that very class? As this is - AFAICT - not the case, what would be a workaround that is in line…
Rappster
  • 12,762
  • 7
  • 71
  • 120
4
votes
1 answer

Is there a destructor in R reference class?

Just as a test: myclass = setRefClass("myclass", fields = list( x = "numeric", y = "numeric" )) myclass$methods( dfunc = function(i) { …
qed
  • 22,298
  • 21
  • 125
  • 196
4
votes
2 answers

Listing explicitly defined methods only (Reference Classes)

Is there a way to list only those methods of a Reference Class, that were explicitly defined in the class definition (as opposed to those methods inherited by "system classes" such as refObjectGenerator or envRefClass)? Example <- setRefClass( …
Rappster
  • 12,762
  • 7
  • 71
  • 120
4
votes
1 answer

Speeding up field access in R reference classes

I have been writing code using R reference classes. However, as I have progressed, the program has become intolerably slow. To demonstrate the problem, take the following example: myClass <- setRefClass( "Class"="myClass", fields=c( …
Brendon
  • 848
  • 8
  • 24
4
votes
2 answers

Retrieving actual source expression that defines a S4 Reference Class from its associated object

In short (actual question) How can I access the actual source code/expression that defines a S4 Reference Class (see ?setRefClass) from the object returned by either getClass("MyClass") or getRefClass("MyClass") (so after it has been sourced, not by…
Rappster
  • 12,762
  • 7
  • 71
  • 120
3
votes
2 answers

ggplot2: How to get a Geom Class to affect ScaleContinuous Class

I have begun to extend ggplot2 and I'm still getting a feel for how the package calls all of its internal functions. I have a new ggproto class that extends one of the current Geom environments. The current model of the class will plot something…
Justin Landis
  • 1,981
  • 7
  • 9
3
votes
0 answers

Docstring format for R reference classes

I have a specific question, but then a "teach a man to fish" question... I am writing an R package using Reference Classes. As described here, I am placing docstrings within each method, which roxygen then picks up and places into the class'…
Eric
  • 1,691
  • 16
  • 24
3
votes
1 answer

Error in initFields(scales = scales) : could not find function "initRefFields"

I have a ggplot2 plotting function as part of my code. The function works fine when the file is sourced as R code, however when I include this function in an R package (and of course I include ggplot2 and scales both in the DESCRIPTION and in the…
Costas B.
  • 186
  • 12
3
votes
1 answer

Document reference class and its methods with roxygen2

I am trying to document a reference class with roxygen2: #' test class #' #' @name myclass #' @export #' @field x A number #' @field y A number #' @method print Print x and y myclass = setRefClass("myclass", fields = list( …
qed
  • 22,298
  • 21
  • 125
  • 196
3
votes
0 answers

How to extend PackageA's reference class in PackageB?

The Problem The diagram below shows my problem: I am trying to create a reference class ClassB in PackageB which extends reference class ClassA in another package PackageA. As soon as ClassB is instantiated, R complains that private (non-exported)…
mchen
  • 9,808
  • 17
  • 72
  • 125
3
votes
1 answer

Save an instance of a reference class in R

I am trying to repeat the examples at the end of the ReferenceClasses help page in R doc: ## a simple editor for matrix objects. Method $edit() changes some ## range of values; method $undo() undoes the last edit. mEdit <- setRefClass("mEdit", …
qed
  • 22,298
  • 21
  • 125
  • 196
3
votes
1 answer

Why is R capricious in its use of attributes on reference class objects?

I am having some trouble achieving consistent behavior accessing attributes attached to reference class objects. For example, testClass <- setRefClass('testClass', methods = list(print_attribute = function(name) print(attr(.self,…
Robert Krzyzanowski
  • 9,294
  • 28
  • 24
3
votes
1 answer

`print` method for ReferenceClass

I have: MyClass <- setRefClass("MyClass" , fields = list(data="numeric")) Let's initialize an object of MyClass: OBJ <- MyClass(data=1:4) ... and print it on screen: OBJ Reference class object of class "MyClass" Field…
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
3
votes
1 answer

Dependency injection: how to use/implement PicoContainer Framework in R

Questions General question How would you go about starting to implement the PicoContainer-Framework in R? Specific question How would the "pico registry (mechanism)" actually look like? I came up with a "poor man's version" that only works for a…
Rappster
  • 12,762
  • 7
  • 71
  • 120
3
votes
0 answers

R Reference Classes - should we use $initFields or $callSuper in constructor?

With regard to R Reference Classes, if we override the default $initialize() constructor, which is considered better practice to initialize fields: $initFields() or $callSuper()? I've considered a few pros/cons: If the default behaviour of…
mchen
  • 9,808
  • 17
  • 72
  • 125
1 2
3
10 11