如何通过字符向量查找R6对象属性

时间:2017-08-28 21:32:58

标签: r r6

我有以下课程,内容如下:

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 $ findMetaFeatures必须能够调用dataSeries $ seriesList [[i]] $ feature,其中feature位于{clusterGIDs,clusterKGs,clusterKPs,clusterKOs}中。默认情况下,使用feature =&#34; clusterKPs&#34;调用findMetaFeatures。在metaSeries $ findMetaFeatures中,我需要一种方法来匹配字符串&#34; clusterKPs&#34;当检查位于self $ seriesList中的某个类型为dataSeries的对象时,使用具有该名称的属性。

1 个答案:

答案 0 :(得分:1)

为了将来参考,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

在定义[[feature]]时,您可以使用[feature]而不是find_features删除结果列表中的一个级别:

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