billlee1231
billlee1231

Reputation: 405

call a method by a character variable in R5 class

I want to know how to call a method of a R5 Class object by using a character variable of the name of the method. Say I have a R5 class object Object.R5 and it has a method called myMethod. The usual way we call the method is just typing:

Object.R5$myMethod()

However if I set a variable

method.name <- 'myMethod'

I'm wondering how can I call the method by using method.name? I tried:

do.call(paste("Object.R5$", method.name, "()", sep=""))

But this prompt an error message saying 'what' must be a character string or a function. I know the answer must be trivial but I'm new to R5 class so... anyone knows the answer please help.

Upvotes: 1

Views: 146

Answers (2)

user1609452
user1609452

Reputation: 4444

eval(parse(text=paste0('Object.R5$',method.name,'()')))

Upvotes: 0

IRTFM
IRTFM

Reputation: 263342

get(method_name, envir=Object.R5)

Upvotes: 4

Related Questions