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
0
votes
0 answers

How to pass a c++/CLI ref class as a function parameter

I have a c++/CLI ref class called LFSparams in my visual c++ code. I want to pass an object of this class as a function parameter: (I get the LFSparameters object as a function parameter from c# code) lfsparms actual_lfsparms =…
Balint
  • 43
  • 6
0
votes
0 answers

Reference classes in R: Get the containing variable

suppose I have a Reference class definition in R which has fields which itself are reference classes: A <- setRefClass("A", fields = list(aVar = "numeric")) B <- setRefClass("B", fields = list(bVarOne = "numeric", bVarTwo = "ANY")) myBinst <-…
SJWard
  • 3,629
  • 5
  • 39
  • 54
0
votes
1 answer

How to subclass a function in R?

I was wondering if it is possible to use Reference Classes to subclass a function in R. For instance, the following > CustomFunction <- setRefClass("CustomFunction", contains = "function") > foo <- CustomFunction() > foo() NULL works OK (does not…
mchen
  • 9,808
  • 17
  • 72
  • 125
0
votes
1 answer

Inheritance leads to error when using reference class

Here is the code: Myclass = setRefClass("Myclass", fields = list( fa = "numeric", fb = "numeric", filename = "character", …
qed
  • 22,298
  • 21
  • 125
  • 196
0
votes
1 answer

Subclass and parent class not in the same file causes error in R

Here is the example from the help page again: mEdit <- setRefClass("mEdit", fields = list( data = "matrix", edits = "list") ) mEdit$methods( initialize =…
qed
  • 22,298
  • 21
  • 125
  • 196
0
votes
1 answer

Missage argument in inheritance of reference class

Here is an example from the official help page: ## a simple editor for matrix objects. Method $edit() changes some ## range of values; method $undo() undoes the last edit. mEdit <- setRefClass("mEdit", fields = list( data =…
qed
  • 22,298
  • 21
  • 125
  • 196
0
votes
1 answer

Loading variable into object from an RData file, is it memory-effecient?

Here is an example: MyClass = setRefClass("MyClass", fields = c("x", "y")) MyClass$methods( initialize = function(xinit=0, yinit=0, filename="") { if(file.exists(filename)) { message("yes, it is there!") …
qed
  • 22,298
  • 21
  • 125
  • 196
0
votes
1 answer

Why are inheritance relationships of R5 classes not defined during R package .onLoad()?

I'm trying to initialize and store some package specific information when a package is first loaded and have found a lot of good information on ways to do this in How to store some package specific information in R and How to run some code on load…
sebkopf
  • 2,335
  • 19
  • 18
0
votes
1 answer

Do reference class generators have to have the same name as the class itself?

When I gave class generators different names to the class itself (there are legitimate reasons for doing this), I started running into problems: myClassGen <- setRefClass("myClass", methods = list(foo = function() baz()) ) myClassGen$methods(baz…
mchen
  • 9,808
  • 17
  • 72
  • 125
0
votes
1 answer

How to get the name of a reference class?

For instance, is there some function getName that allows me to do: > TestA <- setRefClass("TestA") > getName(TestA()) [1] "TestA"
mchen
  • 9,808
  • 17
  • 72
  • 125
0
votes
1 answer

How to use `warning()` in an Reference Class method?

warning() seems to be ignored in reference class methods, although stop() seems to work. That is, TestA <- setRefClass("TestA", methods = list( warnMe = function() warning("Warn!!!"), stopMe =…
mchen
  • 9,808
  • 17
  • 72
  • 125
0
votes
1 answer

How do I index referenceClass objects?

So trying to use referenceClasses for the first time. I figured out how to create an object, but I want an "array" of objects. I usually use data.frames but you can't put an object in a data frame. trying to use lists, but can't figure it out. …
0
votes
1 answer

Inheritance Reference Classes

1) I cant figure out where the Error: Error in args[[1]] : subscript out of bounds comes from, if I try to run the folowing code: Part.initialize<-function(...){ args<-list(...) .self$var1 <- if(is.null(args[["var1"]])) vector() else…
Klaus
  • 1,946
  • 3
  • 19
  • 34
0
votes
0 answers

Warning message by using setMethod

If I run the folowing code in the first time, I get a warning on setMethod: Warning message: In getPackageName(environment(fdef)) : Create package name, ‘2013-09-18 09:29:59’, if nothing there B.getFields<-function(keys){ …
Klaus
  • 1,946
  • 3
  • 19
  • 34
0
votes
1 answer

Return a list of fields of a Reference Class

What is the proper way to define a function which returns a arbitrary number of fields. A.getFields<-function(values){ vars<-getRefClass()$fields() idx<-names(vars) %in% values if(length(fields)<1) return(vars) …
Klaus
  • 1,946
  • 3
  • 19
  • 34
1 2 3
10
11