【问题标题】:R calling a function on several instances of the same reference class objectR在同一引用类对象的多个实例上调用函数
【发布时间】:2021-10-08 17:22:10
【问题描述】:

我有一个引用类 (myRefClass),并创建了几个实例,比如 ref1、ref2 ref3

ref1<- myRefClass()  
ref2<- myRefClass()  
ref3<- myRefClass()  

我正在努力迭代实例(ref1、ref2 和 ref3)并调用类函数。

我能做到:

ref1$say_hello()
ref2$say_hello()
ref3$say_hello()

但是,如果我将对象或名称放在一个列表中,我如何遍历它们并在列表的每个成员上调用函数 say_hello?

listOfObjects <- list(ref1, ref2, ref3)
listOfNames <- list("ref1", "ref2", "ref3")

# iterate through list and call say_hello() neeeded

提前感谢您的帮助

【问题讨论】:

    标签: r class dynamic reference


    【解决方案1】:

    我现在已经使用 get 管理了这个:

    names <- list("ref1", "ref2", "ref3")
    for(ref in names){
      get(ref)$say_hello()
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 2013-03-02
      • 2017-02-11
      • 2021-03-25
      • 1970-01-01
      相关资源
      最近更新 更多