Here's what does not work:
library(R6)
Foo = R6::R6Class(
'Foo',
public = list(
X = NULL,
metadata = NULL,
initialize = function(X, metadata){
self$X = X
self$metadata = metadata
},
`[` = function(selection){
subfoo = Foo$new(X = X[selection],
metadata = self$metadata)
return(subfoo)
}
)
)
Specifically, the [
method is garbage:
> X = matrix(1:8, ncol = 2)
> foo = Foo$new(X, 'blah blah')
> foo[1:2,]
Error in foo[1:2, ] : object of type 'environment' is not subsettable
The desired result is that foo[1:2,]
would be an object of class Foo
just like foo
except that its matrix foo$X
is smaller. Is there a direct way to implement this that exposes the [
operator directly to the user?