[R] Getting names of objects passed with "..."
Mike Meredith
mmeredith at wcs.org
Sat Jun 2 01:42:35 CEST 2007
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.
More information about the R-help
mailing list