The R6 package provides a type of class which is similar to R’s standard reference classes, but it is more efficient and doesn’t depend on S4 classes and the methods package
Questions tagged [r6]
181 questions
23
votes
2 answers
Wrapping shiny modules in R6 classes
I am currently wrapping shiny modules in R6 classes and wanted to hear some opinions about this design.
Basically, I am interested in a clean approach (readable code) and want the classes to allow nesting (see the nesting modules section here). The…

Gregor de Cillia
- 7,397
- 1
- 26
- 43
22
votes
2 answers
Simultaneously access environment from two R sessions
Is it technically possible in R?
I would like to run a shiny instance with prepared R6 object (environment class), use its methods - mostly read only.
While at the same time as shiny app running I would like to call other methods of my R6 -…

jangorecki
- 16,384
- 4
- 79
- 160
18
votes
2 answers
Sub-assign by reference on vector in R
Can I use sub-assign by reference on atomic vectors somehow?
Of course without wrapping it in 1 column data.table to use :=.
library(data.table)
N <- 5e7
x <- sample(letters, N, TRUE)
X <- data.table(x = x)
upd_i <- sample(N, 1L,…

jangorecki
- 16,384
- 4
- 79
- 160
18
votes
1 answer
Proper way to implement S3 dispatch on R6 classes
I have an R6 class and I want to add an S3 method for it. The documentation I found mentioned briefly that in order to use S3 dispatch on R6 you must have class = TRUE, but I couldn't find an example of how it should be done.
I did see empirically…

DeanAttali
- 25,268
- 10
- 92
- 118
17
votes
2 answers
Documenting R6 classes and methods within R package in RStudio
I am struggling with the documentation of an R6 class and its methods. My goal is to get the autocompletion in RStudio for the methods. At the moment, I only get the name of the method but no the help information I normally get using roxygen2…

drmariod
- 11,106
- 16
- 64
- 110
15
votes
2 answers
Static methods in R6 classes
Is there a way to add static methods to R6 classes? For example, a function that can be called like
MyClass$method()
Instead of
myinstance <- MyClass$new()
myinstance$method()

Abiel
- 5,251
- 9
- 54
- 74
13
votes
1 answer
Change initialize method in subclass of an R6 class
Let's say I have a R6 class Person:
library(R6)
Person <- R6Class("Person",
public = list(name = NA, hair = NA,
initialize = function(name, hair) {
self$name <- name
self$hair <- hair
…

Thomas Neitmann
- 2,552
- 1
- 16
- 31
12
votes
2 answers
Multiple inheritance for R6 classes
Actual question
What are my options to workaround the fact that R6 does not support multiple inheritance?
Disclaimer
I know that R is primarily a functional language. However, it does also have very powerful object-orientation built in. Plus: I…

Rappster
- 12,762
- 7
- 71
- 120
12
votes
1 answer
parLapply within R6 classes
I'm looking to use parLapply() on windows within an R6 object and noticed (that in at least some cases) that I do not need to export the R6 functions or data to the nodes.
Here is an example where I can access private methods within…

chandler
- 716
- 8
- 15
11
votes
2 answers
In object-oriented R programming, what is an "active binding"?
In object-oriented R programming (especially Winston Chang's R6 package), what is an active binding?

histelheim
- 4,938
- 6
- 33
- 63
10
votes
1 answer
How to interpret error "elements..... must be named" when sourcing an R6 class?
Code snippets taken from chernan's sample REST queries are utilized to define an R6 class one private method, two public attributes and a constructor:
library(R6)
library(RCurl)
library(RJSONIO)
Symbol <- R6Class("Symbol",
private = list(
#
…

mkk
- 879
- 6
- 19
9
votes
1 answer
R, R6 Operator Overloading
Consider the following:
A = R6::R6Class("ClassA")
B = R6::R6Class("ClassB")
`+.ClassA` = function(o1,o2) o1 #Trivial Example, Usually do something
`+.ClassB` = function(o1,o2) o1 #Trivial Example, Usually do something
a = A$new()
b = B$new()
a +…

Nicholas Hamilton
- 10,044
- 6
- 57
- 88
9
votes
1 answer
How do I tell an R6 class what to do with square brackets?
I have an R6 class that has as an attribute a data.table. Let's say it looks like this:
library(R6)
library(data.table)
foo <- R6Class(
classname = 'foo',
public = list(
dt = NA,
initialize = function(dt) {
self$dt <- dt
}
…

crf
- 1,810
- 3
- 15
- 23
8
votes
1 answer
Reinstantiate an R6 object
Let's say I have a simple abstract R6 class.
myClass <- R6::R6Class(
classname = "myClass",
public = list(
save = function(path) {
saveRDS(self, path)
},
load = function(path) {
object <- readRDS(path)
self <-…

nathaneastwood
- 3,664
- 24
- 41
8
votes
1 answer
R6Class : initialize method raises the error message: "cannot add bindings to a locked environment"
My working environment:
R version: 3.6.3 (64 bits)
OS: Windows 10 (64 bits)
I was working on the following exercice from Hadley Wickham's advanced R book:
Create a bank account R6 class that stores a balance and allows you to
deposit and…

user17911
- 1,073
- 1
- 8
- 18