Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting names from ... (dots)

Tags:

r

In improving an rbind method, I'd like to extract the names of the objects passed to it so that I might generate unique IDs from those.

I've tried all.names(match.call()) but that just gives me:

[1] "rbind"         "deparse.level" "..1"           "..2" 

Generic example:

rbind.test <- function(...) {
  dots <- list(...)
  all.names(match.call())
}

t1 <- t2 <- ""
class(t1) <- class(t2) <- "test"
> rbind(t1,t2)
[1] "rbind"         "deparse.level" "..1"           "..2" 

Whereas I'd like to be able to retrieve c("t1","t2").

I'm aware that in general one cannot retrieve the names of objects passed to functions, but it seems like with ... it might be possible, as substitute(...) returns t1 in the above example.

like image 502
Ari B. Friedman Avatar asked Sep 14 '12 01:09

Ari B. Friedman


2 Answers

I picked this one up from Bill Dunlap on the R Help List Serve:

rbind.test <- function(...) {
    sapply(substitute(...()), as.character)
}

I think this gives you what you want.

like image 68
Tyler Rinker Avatar answered Nov 01 '22 03:11

Tyler Rinker


Using the guidance here How to use R's ellipsis feature when writing your own function?

eg substitute(list(...))

and combining with with as.character

rbind.test <- function(...) {
  .x <-  as.list(substitute(list(...)))[-1]
  as.character(.x)
 }

you can also use

rbind.test <- function(...){as.character(match.call(expand.dots = F)$...)}
like image 23
mnel Avatar answered Nov 01 '22 04:11

mnel