[R] Arguments matching

Stephane DRAY dray at biomserv.univ-lyon1.fr
Fri Nov 26 23:14:14 CET 2004


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:
>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:
>>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/
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>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/
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

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/




More information about the R-help mailing list