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

Implementing standard software design patterns (focus on MVC) in R

Currently, I'm reading a lot about Software Engineering, Software Design, Design Patterns etc. Coming from a totally different background, that's all new fascinating stuff to me, so please bear with me in case I'm not using the correct technical…
Rappster
  • 12,762
  • 7
  • 71
  • 120
48
votes
1 answer

What is the significance of the new Reference Classes?

Apparently John Chambers added Reference Classes to R in version 2.12. There doesn't appear to be much information online yet, but they're calling them R5 classes, which implies they're on a level with S3 and S4 classes. Question: What is a…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
14
votes
3 answers

Private Members in R Reference Class

Is it possible to have private member fields inside of an R reference class. Playing with some of the online examples I have: > Account <- setRefClass( "ref_Account" > , fields = list( > number = "character" > , balance…
akaphenom
  • 6,728
  • 10
  • 59
  • 109
13
votes
1 answer

Dynamically Generate Reference Classes

I'm attempting to generate reference classes within an R package on the fly, and it's proving to be fairly difficult. Here are the approaches I've taken and problems I've run into: I'm creating a package in which I hope to be able to dynamically…
Jeff Allen
  • 17,277
  • 8
  • 49
  • 70
12
votes
1 answer

Roxygen2 - how to @export reference class generator?

For instance, say I have the following package called Test and I want to export class A: # In /R/Test.R: #' @docType package #' @import methods #' @exportClass A A <- setRefClass("A", methods = list(foo = identity)) However, after building and…
mchen
  • 9,808
  • 17
  • 72
  • 125
11
votes
1 answer

Method initialisation in R reference classes

I've noticed some strange behaviour in R reference classes when trying to implement some optimisation algorithm. There seems to be some behind-the-scenes parsing magic involved in initialising methods in a particular which makes it difficult to work…
sbarthelme
  • 111
  • 2
10
votes
2 answers

Non-standard set-functions in R Reference Classes

Is it possible to get the syntax foo$bar(x) <- value to work where foo is a reference class object and bar is a method? I.e. is it possible to do "subset assigment" and have "replacement functions" as methods in Reference Classes? Is the syntax…
Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
10
votes
2 answers

How to create a virtual reference class in R?

I can't find much on virtual/abstract classes in help(ReferenceClasses) - can anyone provide a basic example in creating one? Moreover, how can I specify a virtual method and enforce that child classes must implement it?
mchen
  • 9,808
  • 17
  • 72
  • 125
9
votes
1 answer

Automating assignment in initialize() methods for Reference Classes in R

I'm working with a reference class with a few dozen fields. I've set up an initialize()method that takes a list object in. While some of the fields rely on further computation from list elements, most of the fields are directly assigned from list…
geoffjentry
  • 4,674
  • 3
  • 31
  • 37
8
votes
2 answers

how do I document an R Reference Class?

how do I document the use of member functions of a reference class? if I write a Rd file with a \usage block, how do I avoid the WARNING Functions/methods with usage in documentation object 'XmlDoc' but not in code: $ new I'd expect the \usage…
mariotomo
  • 9,438
  • 8
  • 47
  • 66
8
votes
1 answer

Avoiding consideration of enclosing frames when retrieving field value of a S4 Reference Class

I'm a huge fan of S4 Reference Classes as they allow for a hybrid programming style (functional/pass-by-value vs. oop/pass-by-reference; example) and thus increase flexibility dramatically. However, I think I just came across an undesired behavior…
Rappster
  • 12,762
  • 7
  • 71
  • 120
8
votes
2 answers

Operator overloading for functions in R - strange behavior

Unfortunately things like (f+g)(3) where f and g are both unary functions do not work in R. Hence I tried to overload the "+" operator for unary functions in the following way: "+.function" = function(e1, e2){ return(function(x) e1(x) +…
Patrick Roocks
  • 3,129
  • 3
  • 14
  • 28
7
votes
3 answers

Using "[[ ]]" notation for reference class methods

While experimenting with the new reference classes in R I noticed some odd behaviour if you use the "[[ ]]" notation for methods (X[["doSomething"]] instead of X$doSomething). This notation works for fields, but I initially thought it wouldn't work…
7
votes
1 answer

The equivalent of 'this' or 'self' in R

I am looking for the equivalent of python's 'self' keyword or java's 'this' keyword in R. In the following example I am making an S4 object from a method of a different S4 object and need to pass a pointer to myself. Is there something in the…
jamesatha
  • 7,280
  • 14
  • 37
  • 54
7
votes
3 answers

Reference Classes, tab completion and forced method definition

I am currently writing a package using reference classes. I have come across an issue which from reading various sources: Method initialisation in R reference classes Can't reliably use RefClass methods in Snowfall I gather is caused because…
jdharrison
  • 30,085
  • 4
  • 77
  • 89
1
2 3
10 11