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
2 answers

Setting Global variables inside reference class in R

I'm a bit confused on global variable assignments after reading quite a lot of stack overflow questions. I have gone through Global variables in R and other similar questions I have the following situation. I have 2 global variables current_idx and…
psteelk
  • 1,305
  • 3
  • 16
  • 24
1
vote
2 answers

R reference classes as a field of a reference class

I would like to pass a reference class to a reference class constructor, and assign the passed reference class as a field. However, when I run the code below, I don't understand why I get an error. My questions are: 1) Please can someone kindly…
Sherlock
  • 5,557
  • 6
  • 50
  • 78
0
votes
1 answer

How to write coercion methods

I'm having a bunch of custom-made Reference Classes and would like to write coercion methods for some of them. It'd be nice if a function call would look like this: objectCoerce(src=obj, to="list", ...) where ... is the crucial part as sometimes I…
Rappster
  • 12,762
  • 7
  • 71
  • 120
0
votes
0 answers

RC class method to generate two plots with ggplot2

I have a RC-class with a plot method in r. I have written code to run ggplot() twice, but it will only plot the latest one called. Any suggestions on why this is happening/how to fix it? Below is an example of my structure. testClass <-…
OLGJ
  • 331
  • 1
  • 7
0
votes
1 answer

RC-class structure in r

I am trying to understand RC classes in R (unsuccessfully). I have a simple function which returns an object of class Example. I would now like to use this function in an RC class, where I implement some methods for it. As of now I don't understand…
OLGJ
  • 331
  • 1
  • 7
0
votes
0 answers

Assigning to .self in reference class initialize

I'm trying to define a hybrid initialize function that can either takes a numeric argument or an object of the same class (similar to "copy-constructor" in other languages). My attempt: setRefClass("Temp", fields=list( …
mto_19
  • 135
  • 6
0
votes
1 answer

Referencing the array variables in the Reference class, sorting it using another method, and invoking the sorted values in the case statement

I am trying to call the array variables in the reference class, try to sort them using a user-defined method and call the method onto the case statement that will be invoked if the user chooses a particular number. I wanted to provide the user the…
user15082428
0
votes
1 answer

What is the best way of removing an element from a list of objects in R

While playing with reference classes in R i ran into something that does not feel pretty. If I have a list of objects is there a way to remove an individual item from that list that does not involve finding its index? In the (working) example below…
Per
  • 340
  • 4
  • 9
0
votes
2 answers

unused argument error when printing from RC class method

I am creating an RC class and while trying to print(.self$something) within a class method I am getting: Error in print(.self$something) : unused argument (.self$something) I am sort of new to R, so am I missing something here? This is for an…
avalencia
  • 33
  • 8
0
votes
1 answer

Using missing within initialize method of a reference class

I'm looking to create a generator function for a dummy reference class that would create empty objects of correct classes if arguments are not given. Code #' Class SummaryData #' #' @description Odd class facilitating creation of data frame with #' …
Konrad
  • 17,740
  • 16
  • 106
  • 167
0
votes
1 answer

Overload as.character/paste for reference classes in R

I have built a simple Config reference class in R and I am trying to adjust it so that 'pasting' a list including a Config object will work (something similar to overloading >> operator in C++). Code snippet: Config <- setReferenceClass ("Config" ,…
0
votes
1 answer

How to chain object$method with pipe operator %>%

I am trying to chain several methods (defined in reference class system) from the form of Object_2 <- Object_1$first_method(para_a) Object_3 <- Object_2$second_method(para_b) into, like Object_3 <- Object_1$first_method(para_a) %>%…
Spring
  • 3
  • 1
0
votes
0 answers

Coerce superclass to subclass

I have a class, D, that contains a virtual class, A. I would like to coerce the pieces of D that comprise A into one of two subclasses for A. Is this possible with R's reference classes? The desired output is to have a new object of class D that…
Zelazny7
  • 39,946
  • 18
  • 70
  • 84
0
votes
1 answer

In R reference class, how to define fields as "xts" objects

I am defining a reference class as follow: test = setRefClass( Class = "test", fields = c( edata = "data.frame" ) ) test$methods( getdata = function(newdata,...){ edata <<- newdata } ) And then I use the following code: test1 =…
Benjamin
  • 93
  • 11
0
votes
1 answer

ReferenceClasses and object composition in R

I want to do basic object composition in R and I'm facing this simple problem. I have 2 R5 classes, "Lambda" and "Composition". The class "Composition" has an attribute of class "Lambda". Class "Composition" can't be created:".Object$initialize(...)…