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

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Sat, 23 Jun 2007 10:34:39 -0500

On Sat, 2007-06-23 at 15:54 +0100, 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?
>
> TIA
>
> G

Gavin,

Try this:

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

dat1 <- rnorm(10)
dat2 <- runif(10)

> foo(dat1, dat2)
$dat1
 [1] 0.30314712 1.11273051 1.16002159 -1.69579969 -0.54936868  [6] -0.01931636 -1.56714719 -0.92752592 1.44081430 -0.88249502

$dat2
 [1] 0.53080505 0.55194766 0.42004031 0.23313474 0.08039291 0.69108296  [7] 0.05794077 0.25523083 0.11331677 0.72618992

See ?match.call

HTH, Marc Schwartz



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 Sat 23 Jun 2007 - 15:40:41 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 Sat 23 Jun 2007 - 16:32:12 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.