这是一个简单的代码,它说明了我想要实现的目标:
require(R6)
cTest <- R6Class(
"CTest",
public = list(
a = 10, b=20, c=30,
printX = function(x) {
print(self[x])
}
)
) #"CTest"
myClass <- cTest$new();
for (x in c("a","b", "c"))
myClass$printX("a")
它不起作用。错误讯息:
"Error in self[x] : object of type 'environment' is not subsettable"
为了进行比较,list / data.frames的类似任务有效:
for (x in c("mpg","cyl", "hp"))
print(mtcars[x])
你能帮忙吗?