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

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Sun, 24 Jun 2007 10:23:13 +0100

On Sat, 2007-06-23 at 16:52 +0100, Prof Brian Ripley wrote:
> 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]
> }

Thanks Brian and Marc for this solution. I simplified my example too much, as in reality there are additional arguments after '...', but with a minor change to the solution you provided I got it working.

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

Are there any particular situations (other than the one you show) that you are aware of when this might happen? I will put a Warning section in the Rd page for my function explaining that it might not name the components correctly, so any further examples of where this might not work could be helpful in writing that.

All the best,

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT                          [w] http://www.freshwaters.org.uk/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
R-help_at_stat.math.ethz.ch 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 24 Jun 2007 - 09:28:49 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 24 Jun 2007 - 09:32:22 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.