Skip to content

Obtaining names of further arguments ('...')

3 messages · Arne Henningsen, Duncan Murdoch

#
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".

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

Thanks,
Arne
#
On 09/12/2007 8:28 AM, Arne Henningsen wrote:
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)
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
#
On Sunday 09 December 2007 15:09, Duncan Murdoch wrote
This does exactly what I wanted!
@Duncan: Thank you very much for your quick and helpful answer!

Best wishes,
Arne