[R] Names of objects passed as ... to a function?

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Jun 23 17:52:50 CEST 2007


On Sat, 23 Jun 2007, Gavin Simpson wrote:

> Dear list,
>
> I have a function whose first argument is '...'. Each element of '...'
> is a data frame, and there will be at least 2 data frames in '...'. The
> function processes each of the data frames in '...' and returns a list,
> whose components are the processed data frames. I would like to name the
> components of this returned list with the names of the original data
> frames.
>
> Normally I'd use deparse(substitute()) to do this, but here I do not
> know the appropriate argument to run deparse(substitute()) on, and doing
> this on ... only returns a single "name":
>
>> foo <- function(...)
> +                 deparse(substitute(...))
>> dat1 <- rnorm(10)
>> dat2 <- runif(10)
>> foo(dat1, dat2)
> [1] "dat1"
>
> Can anyone suggest to me a way to get the names of objects passed as
> the ... argument of a function?

That's a little tricky.  The following may suffice:

foo <- function(...)
{
   as.character(match.call())[-1]
}

The problem is that under certain circumstances match.call can give names 
like '..2'

> bar <- function(...) foo(...)
> bar(dat1, dat2)
[1] "..1" "..2"

and I don't know a comprehensive R-level solution to that.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list