Re: [R] Obtaining names of further arguments ('...')

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Sun, 09 Dec 2007 09:09:57 -0500

On 09/12/2007 8:28 AM, Arne Henningsen wrote:
> Hi,
>
> I would like to obtain the names of all objects that are provided as further
> arguments ("...") in a function call. I have created a minimal example that
> illustrates my wish (but isn't useful otherwise):
>
> f1 <- function( ... ) return( deparse( substitute( ... ) ) )
>
> x1 <- 1
> x2 <- 2
> x3 <- 3
>
> f1( x1, x2, x3 )
> [1] "x1"
>
> However, I would like to obtain the names of *all* further arguments,
> i.e. "x1", "x2", and "x3".

Those are not the names of the arguments, you passed them as unnamed arguments. They are expressions, and could well be things like

f1( x1 + x3, mean(x2), 3)

>
> Is this possible in R? Does anybody know how to do this?

Use names(list(...)) to get the names of the arguments. To get what you want, use match.call() to get the call in unevaluated form, and then process it. For example,

f2 <- function( ... ) {

   call <- match.call()
   args <- as.list(call)[-1]
   unlist(lapply(args, deparse))
}

 > f2(x1, x2, x3)
[1] "x1" "x2" "x3"
 > f2( x1 + x3, mean(x2), 3)
[1] "x1 + x3" "mean(x2)" "3"

If your function has arguments other than ... you'll need a bit more work to remove them. They'll end up as the names of the values in the final result.

Duncan Murdoch
Duncan Murdoch



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 09 Dec 2007 - 14:20:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 09 Dec 2007 - 22:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.