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

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 1 14:21:51 CEST 2007


See:
http://tolstoy.newcastle.edu.au/R/e2/help/06/10/2242.html
which we can modify slightly for the case in question like this:

f <- 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))
}
f(a = mean) # a
f(F = function(x)x) # F
f(a = mean, b = sd) # c("a", "b")
f(cos, sin) # c("cos", "sin")
f(a = cos, sin) # c("a", "sin")


On 6/1/07, Mike Meredith <mmeredith at wcs.org> wrote:
>
> Is there a tidy way to get the names of objects passed to a function via the
> "..." argument?
>
> rbind/cbind does what I want:
>
> test.func1 <- function(...) {
>   nms <- rownames(rbind(..., deparse.level=1))
>   print(nms)
> }
>
> x <- "some stuff"
> second <- "more stuff"
> test.func1(first=x, second)
> [1] "first"  "second"
>
> The usual 'deparse(substitute())' doesn't do it:
>
> test.func2 <- function(...) {
>   nms <- deparse(substitute(...))
>   print(nms)
> }
> test.func2(first=x, second)
> [1] "x"
>
> I'm using "nms <- rownames(rbind(...))" as a workaround, which works, but
> there must be a neater way!
>
> rbind/cbind are .Internal, so I can't pinch code from there.
>
> Thanks,  Mike.
>
> --
> View this message in context: http://www.nabble.com/Getting-names-of-objects-passed-with-%22...%22-tf3850318.html#a10906614
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list