如何遍历 R6 类中的子类

How to loop through subclasses within an R6 class

提问人:Greacus 提问时间:6/22/2023 更新时间:6/26/2023 访问量:39

问:

我是 R6 课程的新手,我开始掌握窍门。但是,我目前正在嵌套类,我想遍历特定嵌套类的列表。因此,如下例所示,类的某些元素本身就是类,例如通过类和 元素合并的类。该类还包含感兴趣的子类的列表(作为对它们进行分组的一种方式)。类中的函数遍历列表并捕获感兴趣的特定子类。PersonBodyPartarmlegheadPersonPersonbodyPartsgetVolumePerson

一个小而愚蠢的例子:

BodyPart <- R6::R6Class(classname = "BodyPart",
                        public = list(
                          width = numeric(),
                          volume = numeric(),
                          initialize = function(name = NA, width = 3, volume = 2){
                            self$name <- name
                            self$width <- width
                            self$volume <- volume
                          }
                          
                        )
)

Person <- R6::R6Class(classname = "Person",
                      public = list(
                        name = character(),
                        age = numeric(),
                        bodyParts = list(),
                        arm = environment(),
                        leg = environment(),
                        head = environment(),
                        initialize = function(name = NA, age = NA, bodyParts = list("arm", "leg", "head"), arm = NA, leg = NA, head = NA){
                          self$name <- name
                          self$age <- age
                          self$bodyParts <- bodyParts
                          self$arm <- arm
                          self$leg <- leg
                          self$head <- head
                        }, 
                        getVolume = function(){
                          for(bodyPart in self$bodyParts){
                            volume <- get(bodyPart)$volume
                            print(paste(bodyPart, volume))
                          }
                        }
                      )
)

LongArm <- BodyPart$new()
MediumLeg <- BodyPart$new()
CubeHead <- BodyPart$new()

Person <- Person$new(name = "Jake", age = 20, arm = LongArm, leg = MediumLeg, head = CubeHead)
Person$getVolume()

该代码通常适用于类(在类外部定义的类),但不适用于附加的子类,因为我不知道如何将该部分合并到循环中。Personself$

我知道上面的例子有点愚蠢和间接,但想法是一样的。最终的计划是允许自定义数量的子类 (using ),这些子类可以通过列表选择,从而允许函数一次与自定义数量的特定子类进行交互。...bodyParts

有谁知道如何在 R6 类中选择特定的子类?或者,对于这个问题,不同的方法可能更有效?

提前非常感谢!

R 循环类 嵌套 R6

评论


答:

0赞 Greacus 6/26/2023 #1

我找到了一个非常简单的解决方法: 将所有 bodyparts(子类)一起列出,并让该类将列表作为属性/元素。然后遍历 以获取所需的任何值。self$list

所以这将是:

BodyPart <- R6::R6Class(classname = "BodyPart",
                        public = list(
                          width = numeric(),
                          volume = numeric(),
                          initialize = function(name = NA, width = 3, volume = 2){
                            self$name <- name
                            self$width <- width
                            self$volume <- volume
                          }
                          
                        )
)

Person <- R6::R6Class(classname = "Person",
                      public = list(
                        name = character(),
                        age = numeric(),
                        bodyParts = list(),
                        initialize = function(name = NA, age = NA, bodyParts = NA){
                          self$name <- name
                          self$age <- age
                          self$bodyParts <- bodyParts
                        }, 
                        getVolume = function(){
                          for(bodyPart in self$bodyParts){
                            print(bodyPart$volume)
                          }
                        }
                      )
)

LongArm <- BodyPart$new()
MediumLeg <- BodyPart$new()
CubeHead <- BodyPart$new()

bodyPartsList <- c(LongArm, MediumLeg, CubHead)

Person <- Person$new(name = "Jake", age = 20, BodyParts = bodyPartsList)
Person$getVolume()

希望这也对其他人有所帮助!