Reputation: 893
I would like to implement a generic function:
call_with_parameters <- function(func, parameters) {
call func with parameters and return result
}
that calls the given function func (given as paramter) with a list of parameters, so func must not be able to cope with generic parameters (like ...). As return the call
For example to call: mean(x=1:4, na.rm=TRUE)
as
call_with_parameters(mean, list(x=1:4, na.rm=TRUE))
Any suggestions?
Upvotes: 1
Views: 107
Reputation: 121568
I think, you are looking for do.call
for the construction of function calls.
The function constructs the call and evaluates it immediately( You can also use call
to construct the call and evaluates it later using eval
for example). do.call
takes the arguments
from an object of mode "list" containing all the arguments of function to be evaluated. For example:
do.call("mean", list(x=1:4,na.rm=TRUE))
is equivalent to :
mean(x=1:4,na.rm=TRUE)
Upvotes: 3