Skip to content

Arguments macthing

3 messages · Stéphane Dray

#
Hello list,
I have a question concerning argument matching I have read R Language 
Definition before).
I have a function multxby2 which takes as argument the call to another 
function f1.
I would like to take the values of xx from the call to f1 and use it in 
multxby2.

Here is an example:


multxby2 <- function(callf) {
     appel1<-match.call()
     appel<-as.list(as.call(appel1$callf))

     px<-pmatch(names(appel),"xx")

     if(sum(is.na(px))!=length(appel)) print(appel[[which(!is.na(px))]]*2)
     # else ...

}

f1=function(xx=2,y=3){}

multxby2(f1(x=3))

My problem is due to the various ways that a user can enter its arguments, 
how to be sure to get xx from various call such as:

multxby2(f1(3,2))
multxby2(f1(y=2,3))

Is there a way to do that avoiding a lot of "if" and how to solve the last 
case ? My practical case takes functions with around 20 arguments.

Thanks in advance,

Sincerely.
St??phane DRAY
-------------------------------------------------------------------------------------------------- 

D??partement des Sciences Biologiques
Universit?? de Montr??al, C.P. 6128, succursale centre-ville
Montr??al, Qu??bec H3C 3J7, Canada

Tel : (514) 343-6111 poste 1233         Fax : (514) 343-2293
E-mail : stephane.dray at umontreal.ca
-------------------------------------------------------------------------------------------------- 

Web                                          http://www.steph280.freesurf.fr/
#
I just precise my problem (and correct the subject).

Is there a way to obtain a list where each element has the name of the 
argument (as interpreted by R). The problem with my approach is that if the 
user do not type the name of the argument, this element of the list 
(returned by as.list(as.call(appel1$callf))) has no name.
I suppose that a solution could be obtained using formals, match.args... 
but I did not find it.

Thanks
At 16:30 26/11/2004, Stephane DRAY wrote:
St??phane DRAY
-------------------------------------------------------------------------------------------------- 

D??partement des Sciences Biologiques
Universit?? de Montr??al, C.P. 6128, succursale centre-ville
Montr??al, Qu??bec H3C 3J7, Canada

Tel : (514) 343-6111 poste 1233         Fax : (514) 343-2293
E-mail : stephane.dray at umontreal.ca
-------------------------------------------------------------------------------------------------- 

Web                                          http://www.steph280.freesurf.fr/
#
I have the answer using match.call:



multxby2 <- function(callf) {

     appel1<-match.call()

     appel<-as.list(match.call(eval(appel1$callf[[1]]),call=as.call(appel1$callf)))
     print(appel)

     px<-pmatch(names(appel),"xx")

     if(sum(is.na(px))!=length(appel)) print(appel[[which(!is.na(px))]]*2)
     # else ...

}

f1=function(x=2,y=3){}

multxby2(f1(x=3))
At 16:58 26/11/2004, Stephane DRAY wrote:
St??phane DRAY
-------------------------------------------------------------------------------------------------- 

D??partement des Sciences Biologiques
Universit?? de Montr??al, C.P. 6128, succursale centre-ville
Montr??al, Qu??bec H3C 3J7, Canada

Tel : (514) 343-6111 poste 1233         Fax : (514) 343-2293
E-mail : stephane.dray at umontreal.ca
-------------------------------------------------------------------------------------------------- 

Web                                          http://www.steph280.freesurf.fr/