6

I'm writing some R code and I want to store a list of Function names and what they are for in a dataframe, and then query that dataframe to determine which function to call, but I can't figure out how to do this, or if it's even possible.

As a basic example, let's assume the function name is just stored as a string in a variable, how do I call the function based on the function name stored in that variable?

MyFunc <-function() {
    # Do some stuff...
    print("My Function has been called!!!")
    return(object)
}

FuncName <- "MyFunc()"
Result <- FuncName

I need to make

Result <- FuncName

Work the same as

Result <- MyFunc()

Also, passing objects, or other variables, to the functions is not a concern for what I am doing here, so those () will always be empty. I realize passing variables like this might get even more complicated.

3
  • 1
    Is eval(parse(text = Result)) what you want? Commented Nov 13, 2015 at 23:38
  • 7
    If the answer is parse() you should usually rethink the question. -- Thomas Lumley, R-help (February 2005) Commented Nov 13, 2015 at 23:48
  • ... Furthermore, one should not be attempting to evaluate a function call as a character string in the first place. Commented Nov 13, 2015 at 23:56

2 Answers 2

15

You could use get() with an additional pair of ().

a<-function(){1+1}                                                                                                  
var<-"a"

> get(var)()
[1] 2
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Get was exactly what I was looking for. Plus that extra set of () on the end allows me to pass parameters to the function should I need to(not a requirement now, but nice to have as an option if I need it).
Be careful using get for this purpose. If there is another object in the global environment with the same name as the function (e.g., c <- 1), then get("c") will return the value of the other object (e.g, 1). Try do.call() or match.fun() instead. These two approaches will find only functions (not other objects) associated with the given name (e.g., "c").
5

To get a function from its name, try match.fun, used just like get in the answer provided by @Alex:

> a <- function(x) x+1
> f <- "a"
> match.fun(f)(1:3)
[1] 2 3 4

To call a function directly using its name, try do.call:

> params <- list(x=1:3)
> do.call(f, params)
[1] 2 3 4

The advantage to do.call is that the parameters passed to the function can change during execution (e.g., values of the parameter list can be dynamic at run time or passed by the user to a custom function), rather than being hardwired in the code.

Why do I suggest match.fun or do.call over get? Both match.fun and do.call will make sure that the character vector ("a" in the above examples) matches a function rather than another type of object (e.g., a numeric, a data.frame, ...).

Consider the following:

# works fine
> get("c")(1,2,3)
[1] 1 2 3

# create another object called "c"
> c <- 33
> get("c")
[1] 33

#uh oh!!
> get("c")(1,2,3)
Error: attempt to apply non-function

# match.fun will return a function; it ignores
# any object named "c" that is not a function.
> match.fun("c")(1,2,3)
[1] 1 2 3

# same with do.call
> do.call("c", list(1,2,3))
[1] 1 2 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.