Closed terrytangyuan closed 8 years ago
You can use do.call(Class1$addKV, list("key1", "value1"))
.
Sure, but I meant something like this do.call("AnyClassName$addKV", list("key1", "value1"))
.
It depends on which part you want to be a string. If it's the object:
objName <- "AnyClassName"
obj <- get(objName)
do.call(obj$addKV, list("key", "value"))
If it's the method:
methodName <- "addKV"
do.call(obj[[methodName]], list("key", "value"))
If you want to use a single string for both, you could use eval
, although it's a little gross:
f <- eval("AnyClassName$addKV")
do.call(f, list("key", "value"))
Got it! This is very helpful! Thanks!
Oops, the eval code should be:
f <- eval(parse(text = "AnyClassName$addKV"))
Yep got the idea! Thanks!
For example, I have a
Class1$addKV("key1", "value1")
function. Is there a way to usedo.call()
to call it since I want to make this more general.Thanks!