开发者

how to wrap a function that only takes individual elements to make it take a list [duplicate]

This question already has an answer here: Can you pass a vector to a vararg?: Vector to sprintf (1 answer) Closed 9 years 开发者_如何转开发ago.

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.


You are looking for do.call:

f <- function(x,y,z)x+y+z
do.call(f,list(1,2,3))
[1] 6
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜