我有下面的类,它的开头如下:
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){实际上,metaSeries$seriesList将被初始化为dataSeries类型的列表。dataSeries$findMetaFeature必须能够调用dataSeries$seriesList[i]$feature,其中功能位于{clusterGID,clusterKGs,clusterKPs,clusterKOs}中。默认情况下,使用feature="clusterKPs“调用findMetaFeatures。在metaSeries$findMetaFeature中,当检查位于self$seriesList中的某个类型为dataSeries的对象时,我需要一种方法将字符串"clusterKPs“与具有该名称的属性进行匹配。
发布于 2017-08-29 07:44:30
对于将来的参考,Colin FAY是正确的;您应该提供更多的上下文,并且通常至少应该提供重现您的问题所需的最少代码,以帮助其他人真正知道您需要帮助的内容。
也就是说,我相信您正在询问的问题的答案相对简单:您可以通过使用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"这可以是轻松访问同一R6类的多个对象的同一字段的一种方法。
编辑:
看过您的一些代码后,我会更清楚地了解您想要实现的目标。答案是一样的,您只需在R6类的公共函数中实现它,如下所示:
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在定义find_features时,您可以通过使用[[feature]]而不是[feature]来消除结果列表的一个级别
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 6https://stackoverflow.com/questions/45927667
复制相似问题