Dear r-list folks, I have a problem which has been bugging me for a while now and I was hoping someone out there might be able to help. If I have a user-defined function with an indeterminate number of arguments, using the well-known "..." construct, how can I get the function to return the names of the items which were the arguments of the function as part of the function's output? This is easily done where there is a fixed number of arguments: e.g: aa<-c(1,2,3,4,5) bb<-c(6,7,8,9,10) argument.out<-function(object1,object2){ name1<-deparse(substitute(object1)) name2<-deparse(substitute(object2)) output<-c(name1,name2) return(output) } argument.out(aa,bb) How can I produce a similar results from a function where there is an indeterminate number of arguments i.e: argument.list<-function(object,...){ Any suggestions or solutions would be much appreciated. Kind regards, Michael Scroggie
Michael.Scroggie at nre.vic.gov.au writes:> Dear r-list folks, > > I have a problem which has been bugging me for a while now and I was hoping > someone out there might be able to help. > > If I have a user-defined function with an indeterminate number of > arguments, using the well-known "..." construct, how can I get the > function to return the names of the items which were the arguments of the > function as part of the function's output? This is easily done where there > is a fixed number of arguments: e.g: > > aa<-c(1,2,3,4,5) > bb<-c(6,7,8,9,10) > > argument.out<-function(object1,object2){ > name1<-deparse(substitute(object1)) > name2<-deparse(substitute(object2)) > output<-c(name1,name2) > return(output) > } > argument.out(aa,bb) > > > How can I produce a similar results from a function where there is an indeterminate number of arguments i.e: > > argument.list<-function(object,...){ > > Any suggestions or solutions would be much appreciated.match.call() is your friend. E.g. f <- function(...) sapply(as.list(match.call())[-1], deparse) f(fee=foo,fie=bar,foe=baz) actually, as.list is superfluous: f <- function(...) sapply(match.call()[-1], deparse) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
>-----Original Message----- >From: r-help-admin at stat.math.ethz.ch >[mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of >Michael.Scroggie at nre.vic.gov.au >Sent: Monday, February 17, 2003 4:32 PM >To: r-help at stat.math.ethz.ch >Subject: [R] returning argument names > > >Dear r-list folks, > >I have a problem which has been bugging me for a while now and >I was hoping someone out there might be able to help. > >If I have a user-defined function with an indeterminate number >of arguments, using the well-known "..." construct, how can I >get the function to return the names of the items which were >the arguments of the function as part of the function's >output? This is easily done where there is a fixed number of >arguments: e.g: > >aa<-c(1,2,3,4,5) >bb<-c(6,7,8,9,10) > >argument.out<-function(object1,object2){ >name1<-deparse(substitute(object1)) >name2<-deparse(substitute(object2)) >output<-c(name1,name2) >return(output) >} >argument.out(aa,bb) > > >How can I produce a similar results from a function where >there is an indeterminate number of arguments i.e: > >argument.list<-function(object,...){ > >Any suggestions or solutions would be much appreciated. > >Kind regards, > > >Michael ScroggieSomebody jump in and correct me if wrong but I believe that you can use the following in the body of your function: { ... dotargs <- list(...) ... } You can then access 'dotargs', which will be a list containing the arguments and their names, if you specified names. Of course the length of dotargs will be the number of additional arguments specified. A quick sample that just returns some arbitrary arguments: dottest <- function(Arg1, ...) { dotargs <- list(...) return(dotargs) }>dottest(1, b = 2, MyArg = 3, d = "something")$b [1] 2 $MyArg [1] 3 $d [1] "something" Note that 'Arg1' is explicitly defined in the function, so it is not part of 'dotargs'. You would need to add that into your function return or print values. Hope that helps, Regards, Marc Schwartz