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

How to look up an R6 object attribute by character vector

我有以下 class 开头如下:

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,

我还有一个 class 开头如下:

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="clusterKPs" 调用 findMetaFeatures。在 metaSeries$findMetaFeatures 中,在检查位于 self$seriesList.

中的某些 dataSeries 类型的对象时,我需要一种方法将字符串 "clusterKPs" 与具有该名称的属性相匹配

备查,Colin FAY是对的;您应该提供更多的上下文,并且通常应该至少提供重现您的问题所需的最少代码量,以帮助其他人真正了解您需要什么帮助。

就是说,我相信您要问的问题的答案相对简单:您可以使用 [=13= 使用字符向量访问 R6 class 对象的 public 字段]:

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 的许多对象的相同字段的方法 class。

编辑:

看了你的一些代码后,我更清楚地看到了你想要完成的事情。答案是一样的,您只需在 R6 class 的 public 函数中实现它,如下所示:

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 6