gagaouthu
gagaouthu

Reputation: 329

Evaluate a function whose arguments are stored in a list

I would like to evaluate a certain function g inside of a function f, but the arguments of g are given to f in a list. f and g are defined in this way:

g <- function(a,b){
  a+b
}
f <- function(x, y, l){
  # do some stuff on x and y
  z <- g(l)  # Not working
  # do some stuff on x, y and z
}

I would like to run f in this way:

f(xx, yy, list(a=aa, b=bb)

where aa, bb, xx, yy are some R objects.

How could I do that?

Upvotes: 2

Views: 69

Answers (1)

Pop
Pop

Reputation: 12411

You should use do.call function in this way:

f <- function(x, y, l){
  # do some stuff on x and y
  z <- do.call(g,l)
  # do some stuff on x, y and z
}

Upvotes: 2

Related Questions