Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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.

like image 554
stevejb Avatar asked Apr 26 '10 14:04

stevejb


People also ask

How do I make a list in Excel without duplicates?

On the Data menu, point to Filter, and then click Advanced Filter. In the Advanced Filter dialog box, click Filter the list, in place. Select the Unique records only check box, and then click OK. The filtered list is displayed and the duplicate rows are hidden.

How do I copy only unique values in Excel?

In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.


1 Answers

You are looking for do.call:

f <- function(x,y,z)x+y+z
do.call(f,list(1,2,3))
[1] 6
like image 136
Aniko Avatar answered Sep 27 '22 18:09

Aniko