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

From: Mike Meredith <mmeredith_at_wcs.org>
Date: Fri, 01 Jun 2007 16:42:35 -0700 (PDT)

Sorry, I responded a bit too hastily last night, without testing the two functions properly.

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

Indeed! 'f1' only works if at least one of the arguments is named. Otherwise 'nm' is NULL and 'nchar(nm[i])' fails. 'f2' seems to have all combinations covered. Thanks again!!

> 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"



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.
-- 
View this message in context: http://www.nabble.com/Getting-names-of-objects-passed-with-%22...%22-tf3850318.html#a10922269
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.
Received on Fri 01 Jun 2007 - 23:46:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 02 Jun 2007 - 00:31:49 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.