如何通过字符向量查找R6对象属性
我的课程如下:如何通过字符向量查找R6对象属性,r,r6,R,R6,我的课程如下: dataSeries <- R6Class("dataSeries", public = list( geoAccession = NULL, species = NULL, tissue = NULL, seuratObjectPath = NULL,
dataSeries <- R6Class("dataSeries",
public = list(
geoAccession = NULL,
species = NULL,
tissue = NULL,
seuratObjectPath = NULL,
markerType = NULL,
clusters = NULL,
clusterTable = NULL,
clusterSymbols = NULL,
clusterPVs = NULL,
clusterGIDs = NULL,
clusterKGs = NULL,
clusterKPs = NULL,
clusterKOs = NULL,
metaSeries <- R6Class("metaSeries",
public = list(
seriesList = NULL,
initialize = function(dataDir="Data/Data_Series/Current"){
browser()
if(!is.null(dataDir)){
toProcess = list.files(dataDir,full.names = T)
self$seriesList = vector("list", length(toProcess))
count = 1
for(file in toProcess){
series <- readRDS(file)
self$seriesList[[count]] <- series
count = count + 1
}
}
},
findMetaFeatures = function(feature="clusterKPs", rank=3, plot=TRUE){
dataSeries供将来参考,是正确的;您应该提供更多的上下文,并且通常应该至少提供再现您的问题所需的最少代码量,以帮助其他人真正了解您需要的帮助
也就是说,我相信您要问的问题的答案相对简单:您可以使用as.list
,使用字符向量访问R6类对象的公共字段:
library(R6)
myClass <- R6Class('myClass',
public = list(
someData = NULL,
initialize=function(someData = NA){
self$someData <- someData
},
set_someData = function(val){
self$someData <- val
}
)
)
myObject <- myClass$new(someData='a')
class(myObject)
[1] "myClass" "R6"
class(myObject$someData)
[1] "character"
myObject$someData
[1] "a"
as.list(myObject)[['someData']]
[1] "a"
lappy(myObject,…)
?能否提供用于生成R6对象的代码?这不清楚你想要检索什么。谢谢@Colin FAY,我已经更新了OP
library(R6)
myClass <- R6Class('myClass',
public = list(
someData = NULL,
initialize=function(someData = NA){
self$someData <- someData
},
set_someData = function(val){
self$someData <- val
},
find_features = function(feature='otherData'){
if ( !('list' %in% class(self$someData)) ) {
if ( !('mySubClass' %in% class(self$someData)) ){
stop('someData does not have the feature')
}
return(as.list(self$someData)[feature])
}
return(lapply(self$someData, function(x){
as.list(x)[feature]
}))
}
)
)
mySubClass <- R6Class('mySubClass',
public = list(
otherData = NULL,
initialize = function(otherData = NA){
self$otherData <- otherData
}
)
)
mySubObject1 <- mySubClass$new(otherData=1:3)
mySubObject2 <- mySubClass$new(otherData=4:6)
myObject <- myClass$new(someData=list(mySubObject1, mySubObject2))
myObject$find_features()
[[1]]
[[1]]$otherData
[1] 1 2 3
[[2]]
[[2]]$otherData
[1] 4 5 6
myClass <- R6Class('myClass',
public = list(
someData = NULL,
initialize=function(someData = NA){
self$someData <- someData
},
set_someData = function(val){
self$someData <- val
},
find_features = function(feature='otherData'){
if ( !('list' %in% class(self$someData)) ) {
if ( !('mySubClass' %in% class(self$someData)) ){
stop('someData does not have the feature')
}
return(as.list(self$someData)[[feature]])
}
return(lapply(self$someData, function(x){
as.list(x)[[feature]]
}))
}
)
)
myObject <- myClass$new(someData=list(mySubObject1, mySubObject2))
myObject$find_features()
[[1]]
[1] 1 2 3
[[2]]
[1] 4 5 6