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

R Reference Class issue

I am trying to create a simple reference class in R. Here is my code (R beginner): MyClass <- setRefClass("MyClass", fields = list(a = "numeric", b = "numeric"), …
Sherlock
  • 5,557
  • 6
  • 50
  • 78
6
votes
1 answer

parallel computations on Reference Classes

I have a list of fairly large objects that I want to apply a complicated function to in parallel, but my current method uses too much memory. I thought Reference Classes might help, but using mcapply to modify them doesn't seem to work. The…
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
6
votes
1 answer

Defining default field values for instances of S4 Reference Classes

How can I define the fields' default values of S4 Reference Classes instances? For regular S4 Classes, there's the prototype argument: setClass("Test_1", representation( x.1="numeric", x.2="logical", x.3="matrix" …
Rappster
  • 12,762
  • 7
  • 71
  • 120
5
votes
1 answer

define class methods and class variables in R5 reference class

I want to know the correct way to define the class methods and class variable in R5 reference class. Here is an example: > # define R5 class XX > # member variable: ma > # member method: mfa > XX <- setRefClass("XX", + fields = list(ma =…
kohske
  • 65,572
  • 8
  • 165
  • 155
5
votes
0 answers

Warnings thrown when accessing methods of reference class through RStudio

The code and the warnings: tinyclass <- setRefClass("TinyClass", methods = list(doNothing=function(){})) tc <- tinyclass() tc$doNothing() NULL Warning messages: 1: In installClassMethod(value, self, field, selfEnv, thisClass) : method…
Nick
  • 51
  • 2
5
votes
2 answers

In R, is it possible to suppress "Note: no visible binding for global variable"?

I'm wondering if its possible to suppress these outputs in R which are cluttering up the console: Note: no visible binding for global variable '.->ConfigString' Note: no visible binding for '<<-' assignment to 'ConfigString' Here is the code (its…
Contango
  • 76,540
  • 58
  • 260
  • 305
5
votes
1 answer

R packages - should I import the `methods` package?

I'm using setRefClass to create classes and since is part of the methods package, I presumed you need to declare this dependency as an import. However, the following minimal example fails Rcmd.exe check when importing methods: #' @docType package #'…
mchen
  • 9,808
  • 17
  • 72
  • 125
5
votes
1 answer

R reference classes - how to determine if you're in an inherited method?

For a given reference class method, how can I determine if it is inherited? More generally, how to determine how far up the inheritance tree I am? For example, if my setup is: A <- setRefClass("A", methods = list(foo = function()…
mchen
  • 9,808
  • 17
  • 72
  • 125
5
votes
2 answers

Reference Class with custom field classes in R?

I would like to use a custom reference class inside another reference class, but this code fails: nameClass <- setRefClass("nameClass", fields = list(first = "character", last = "character"), …
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
5
votes
2 answers

Using R reference classes to pass values from one window to another in a GUI

I am making a GUI in R using gWidgets. Until now I have been passing values from one window to another via the global environment. Using the global environment is simple to implement but not ideal. One problem is that R CMD check complains about…
5
votes
1 answer

Can a Reference Class be made to Log Calls

I have a question about Reference Classes. My question is in the context of an R package I am developing rCharts. It uses reference classes to create interactive plots from R. Creating a plot involves a series of calls. Here is an example, where a…
Ramnath
  • 54,439
  • 16
  • 125
  • 152
5
votes
1 answer

R testing for reference classes

Is there a quick and dirty way to test whether an instance is from a reference class? The standard R object tests yield the following - but nothing that seems to exclusively mark a reference class. classy <- setRefClass('classy', fields = list( …
Mark Graph
  • 4,969
  • 6
  • 25
  • 37
5
votes
1 answer

Copying R5 reference classes with a locked variable

I can copy an R5 reference class when I have not locked one of the fields, but it does not copy if one of the fields is locked. Example code follows (with the lock call commented out). My question: Why can't I make a copy of the instance with a…
Mark Graph
  • 4,969
  • 6
  • 25
  • 37
4
votes
2 answers

Update a Reference Class method

I am currently developping Reference classes (R5) for large objects which take time to produce, and i wonder if someone knows a better way to develop methods than redefining the class with setRefClass and reproduce an object each time i update a…
maressyl
  • 983
  • 6
  • 11
4
votes
1 answer

How to control inheritance when dynamically extending Reference Classes

In a webcrawler/webscraper-setting, I'd like to dynamically extend my base Reference Class URL in order to be able to write specific methods for respective hosts/domains. Just to be clear, by dynamically I mean something like "automatically generate…
Rappster
  • 12,762
  • 7
  • 71
  • 120
1
2
3
10 11