0

How do you call a R6 method dynamically i.e the method name is held as a string variable:

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")

1 Answers1

0

You can use

test_obj[[method_to_call]]("hello")

In general you want to use [[]] when sub-setting with variables. $ only works with literal values.

MrFlick
  • 195,160
  • 17
  • 277
  • 295