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

Marc Schwartz marc_schwartz at comcast.net
Sat Jun 23 17:34:39 CEST 2007


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



More information about the R-help mailing list