[R] Getting names of objects passed with "..."

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 1 17:49:26 CEST 2007


On 6/1/07, Mike Meredith <mmeredith at wcs.org> wrote:
>
>
> Thanks very much to all of you. It looks like 'match.call' is the key, and
> both Brian's and Gabor's solutions work fine. --- Mike.

Brian's is shorter but I think the one in my post is a bit more robust:

> f1 <- function(...) {
+    m <- as.list(match.call(expand.dots=TRUE))[-1]
+    nm <- names(m)
+    for(i in seq_along(m)) if(!nchar(nm[i])) nm[i] <- deparse(m[[i]])
+    nm
+ }
>
> f2 <- function(...) {
+ x <- list(...)
+ if (is.null(names(x))) names(x) <- ""
+ names(x)[names(x) == ""] <- NA
+ mc <- match.call()[-1]
+ ifelse(is.na(names(x)), as.character(mc), names(x))
+ }
>
> f1(sin, cos)
Error in if (!nchar(nm[i])) nm[i] <- deparse(m[[i]]) :
        argument is of length zero
> f2(sin, cos)
[1] "sin" "cos"



More information about the R-help mailing list