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

Duncan Murdoch murdoch at stats.uwo.ca
Sun Dec 9 15:09:57 CET 2007


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



More information about the R-help mailing list