这是不起作用的:
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)
}
)
)
具体来说,该[
方法是垃圾:
> 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
期望的结果是,除了它的矩阵更小之外,这foo[1:2,]
将是一个类的对象Foo
。有没有直接的方法来实现这一点,将操作员直接暴露给用户?foo
foo$X
[