tags:

views:

94

answers:

1

Hello,

Say I have a function handed to me that I cannot change and must use as is. This function takes several objects in the form of

oldFunction( object1, object2, object3, ...)

where ... are other arguments. I want to write a wrapper to take a list of objects. My idea was this.

sjb.ListWrapper <- function(myList,...) {
  lLen <- length(myList)
  myStr <- ""
  for( i in 1:lLen) {
    myStr <- paste(myStr, "myList[[", i , "]],",sep="")
  }

  myCode <- paste("oldFunction(", myStr, "...)")
  eval({myCode})
}

However, the issue is that I want to use this from Sweave and I need the output of oldFunction to be printed. What is the right way to do this?

Thanks.

+9  A: 

You are looking for do.call:

f <- function(x,y,z)x+y+z
do.call(f,list(1,2,3))
[1] 6
Aniko
Thank you. No need to reinvent the wheel then. :)
stevejb