48

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 reference class, and how does this fit in with existing class types?

Andrie
  • 170,733
  • 42
  • 434
  • 486
Ari B. Friedman
  • 69,285
  • 35
  • 174
  • 232
  • 2
    I think "R5" was a kind of joke made during an Rcpp presentation - partly as the need for a brand new R was being discussed at the time, I don't think it's a serious name. The Reference Classes fit squarely in R's S4 afaik. Searching for "R5" in SO [r] chat gives http://chat.stackoverflow.com/search?q=R5&room=106 – mdsumner Feb 28 '11 at 00:57
  • 15
    Reference based means the objects are mutable - changes don't make copies. – hadley Feb 28 '11 at 03:49
  • 1
    In fact, much depends on what you want to do. S4 comes in handy sometimes (especially the dispatching and more rigorous check of objects). Reference classes seem ideal for work with huge datasets that need transformation prior to analysis. But for the rest, I'd like my objects untouched. – Joris Meys Feb 28 '11 at 14:25
  • 1
    @Joris: Makes sense. It is a bit frustrating that there are now three not-really-compatible OO systems for R. I'd rather learn one and learn it right. So I tend to stick with S3 classes. Even I can see the limitations of them, but they're so simple to use it's seductive. If S4 classes got more consistent praise I would put in the effort to really learn them. – Ari B. Friedman Feb 28 '11 at 20:59
  • 1
    It looks like Reference Classes are progressing nicely in R core. `trace` now (R 2.13.1) supports them. – Ari B. Friedman Aug 11 '11 at 00:18
  • And for more info on RCs in other languages: http://programmers.stackexchange.com/questions/57068/why-are-objects-passed-by-reference – Ari B. Friedman Aug 11 '11 at 02:44

1 Answers1

35

The request for documentation for ReferenceClasses comes up every now and then, for example on the r-devel list. The best answer so far is to actually look at what help(ReferenceClasses) gives you---which is a pretty decent start.

Then there are a few presentations:

And as noted by mdsummer in the comment, R5 was a short-lived joke. There was already another R-related project called R5, and John much prefers ReferenceClasses. And I reckon they are here to stay. People use them already as e.g. Jeff Horner is his new Rack package.

mnel
  • 110,110
  • 27
  • 254
  • 248
Dirk Eddelbuettel
  • 347,098
  • 55
  • 623
  • 708