How to call a function using the character string of the function name in R?

Viewed 37131

I am trying to call a function with a given string of the function name.

E.g.

print(funcList)
[[1]]
`*`

[[2]]
sin

works:

mult <- `*`
mult(5,6)
[1] 30

doesn't work:

func1 <- funcList[[1]]
func1(5,6)

func2 <- funcList[[2]]
func2(1.2)

So is it possible to call all of the functions in the functionList?

3 Answers

you could also use match.fun

> functionlist <- list("*","sin")
> f <- match.fun(functionlist[[1]])
> f(5,6)
[1] 30
Related