[R] using argument names (of indeterminate number) within a function

Dirk Enzmann dirk.enzmann at jura.uni-hamburg.de
Tue Jul 19 17:29:25 CEST 2005


Although I tried to find an answer in the manuals and archives, I cannot 
solve this (please excuse that my English and/or R programming skills 
are not good enough to state my problem more clearly):

I want to write a function with an indeterminate (not pre-defined) 
number of arguments and think that I should use the "..." construct and 
the match.call() function. The goal is to write a function that (among 
other things) uses cbind() to combine a not pre-defined number of 
vectors specified in the function call. For example, if my vectors are 
x1, x2, x3, ... xn, within the function I want to use cbind(x1, x2) or 
cbind(x1, x3, x5) or ... depending on the vector names I use in the 
funcion call. Additionally, the function has other arguments.

In the archives I found the following thread (followed by Marc Schwartz)

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/15186.html
[R] returning argument names from Peter Dalgaard BSA on 2003-04-10 (stdin)

that seems to contain the solution to my problem, but I am stuck because 
  sapply(match.call()[-1], deparse) gives me a vector of strings and I 
don't know how to use the names in this vector in the cbind() function.

Up to now my (clearly deficit) function looks like:

test <- function(..., mvalid=1)
{
   args = sapply(match.call()[-1], deparse)
# and here, I don't know how the vector names in args
# can be used in the cbind() function to follow:
#
# temp <- cbind( ???
   if (mvalid > 1)
   {
#  here it goes on
   }
}

Ultimately, I want that the function can be called like
test(x1,x2,mvalid=1)
or
test(x1,x3,x5,mavlid=2)
and that within the function
cbind(x1,x2)
or cbind(x1,x3,x5)
will be used.

Can someone give and explain an example / a solution on how to proceed?

*************************************************
Dr. Dirk Enzmann
Institute of Criminal Sciences
Dept. of Criminology
Edmund-Siemers-Allee 1
D-20146 Hamburg
Germany

phone: +49-040-42838.7498 (office)
        +49-040-42838.4591 (Billon)
fax:   +49-040-42838.2344
email: dirk.enzmann at jura.uni-hamburg.de
www: 
http://www2.jura.uni-hamburg.de/instkrim/kriminologie/Mitarbeiter/Enzmann/Enzmann.html




More information about the R-help mailing list