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

`ReferenceClasses` and Imports / Depends in the DESCRIPTION

I am developing a package containing a ReferenceClass that has a field of data.table class (defined in the data.table package): MyRC <- setRefClass("MyRC", fields = list(myfield="data.table")) When I write into package DESCRIPTION file: Depends: …
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
1
vote
2 answers

In R, how to print values of fields in a ReferenceClass?

I have a ReferenceClass in R. How can I add a method "print()" to it, which will print the values of all of the fields in the class?
Contango
  • 76,540
  • 58
  • 260
  • 305
1
vote
2 answers

How to tell an R reference class object to update its method definitions?

I'm looking for a way to tell an instance of a reference class to forget one of its method definitions. For example, I create the class MyReferenceClass and an instance called my_object I can call the method print_hello and everything…
nachocab
  • 13,328
  • 21
  • 91
  • 149
1
vote
1 answer

Extending original error messages

Is there a way to directly extend/augment/overwrite the original error messages that are thrown when calls to functions that I didn't write myself fail (i.e. functions from base R and contributed packages)? Example Consider the following Reference…
Rappster
  • 12,762
  • 7
  • 71
  • 120
1
vote
1 answer

Auto update of a field (data member) in R reference class

Any equivalent as the decorator @property in python? That is to have one field automatically updated (on access) due to an update of another field, instead of recomputing it before access. UPDATE 2014-02-06 By defining "one" field as an…
xb.
  • 1,617
  • 11
  • 16
1
vote
1 answer

Roxygen2 - "argument is of length zero" error when documenting reference class

To demonstrate a minimal case, say I have the following package: #' @docType package #' @import methods #' @title MyTitle #' @description MyDescription #' @details MyDetails #' @export A <- setRefClass("A") When I roxygenize (in RStudio, before a…
mchen
  • 9,808
  • 17
  • 72
  • 125
1
vote
1 answer

Functional interfaces for reference classes

I am trying to figure out the conceptual implications of providing a functional user interface to reference classes (or indeed S4/S3 classes). In short, I am concerned that if I write code that looks like as below, then it is equivalent to writing…
tchakravarty
  • 10,736
  • 12
  • 72
  • 116
1
vote
0 answers

How to determine the type of a object of a reference class

I have list of objects of different types of reference classes. How to determine the type of the folowing class? Should I use the function class? A<-setRefClass(Class = "A", fields = c(var1 =…
Klaus
  • 1,946
  • 3
  • 19
  • 34
1
vote
1 answer

Initialize a arbitrary number of fields

Is the function initFields not a good way to initialize arbitrary fields of a reference class? If not what would you suggest? And how could I handle the a field of type "ANY", because for the string "ANY" I cant do a call like…
Klaus
  • 1,946
  • 3
  • 19
  • 34
1
vote
1 answer

Constructor reference classes

I like to change the the folowing src in that way, that the initialize function looks like certain contructors. I would like to change the folowing example Part.initialize<-function(...){ args<-list(...) …
Klaus
  • 1,946
  • 3
  • 19
  • 34
1
vote
2 answers

Programming with Reference Class

How to define several polymorph constructors and functions like function Add( x, y : Integer ) : Integer; begin Add := x + y end; function Add( s, t : String ) : String; begin Add := Concat( s, t ) end; begin Writeln(Add(1, 2)); …
Heiko
  • 83
  • 1
  • 8
1
vote
0 answers

Building R package with Reference objects fails

I recently rewrote a package to use the new(er) R reference class objects. I've exported the three classes using export() in the NAMESPACE file so as far as I'm aware that should work. However when I test build the package I get an error at lazy…
SJWard
  • 3,629
  • 5
  • 39
  • 54
1
vote
1 answer

Operator overloading in R reference classes

I'm relatively new at OOP and need advice: What is the best way to overload the Arithmetic generic operators in reference classes in R? For example, suppose I define bar <- setRefClass( "foo", fields=list(a="numeric", b="numeric" )) If I try the…
1
vote
1 answer

Returning copy of data.table from ReferenceClass method

I try to return copies of data.tables from methods of a ReferenceClass: dummy <- setRefClass( "dummy", fields = list( dt = "data.table" ), methods = list( initialize = function( df ){ if( !missing( df ) ){ dt <<-…
Beasterfield
  • 7,023
  • 2
  • 38
  • 47
1
vote
1 answer

setRefClass pass by value, pass by reference

I suspect i am not understanding all the aspects of setRefClass in R. Lets say I have an instance of a setRefClass initialized. I want to create the variable X so that this variable is equal to a copy of the instance or refers to the instance of…
pam
  • 676
  • 1
  • 7
  • 27