【问题标题】:Calling R6 method dynamically where method name is stored in string variable动态调用 R6 方法,其中方法名称存储在字符串变量中
【发布时间】:2020-04-21 17:15:11
【问题描述】:

如何动态调用 R6 方法,即方法名称作为字符串变量保存:

Test <- R6Class("Test",
  public = list(
      char_to_upper = function(var) { toupper(var) }
  ))

# create test object
test_obj <- Test$new()

# method name store in variable
method_to_call <- "char_to_upper"

# call method
test_obj$method_to_call("hello")

【问题讨论】:

    标签: r r6


    【解决方案1】:

    你可以使用

    test_obj[[method_to_call]]("hello")
    

    一般来说,在使用变量进行子设置时,您希望使用[[]]$ 仅适用于文字值。

    【讨论】:

      猜你喜欢
      • 2018-02-08
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 2010-11-27
      • 2013-12-06
      相关资源
      最近更新 更多