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 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/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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 > > GGavin, 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
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