Skip to content

Getting names of objects passed with "..."

7 messages · Henrik Bengtsson, Brian Ripley, Mike Meredith +1 more

#
I use:

foo <- function(...) {
  args <- list(...);
  names(args);
}

/Henrik
On 6/1/07, Mike Meredith <mmeredith at wcs.org> wrote:
#
On Fri, 1 Jun 2007, Henrik Bengtsson wrote:

            
But that does not do what was asked: it gets the argument names, not the 
object names.  (Did you actually try it?)  It looks from the example that 
he wants the argument name if there is one otherwise the 
deparsed argument value, but clarification would be helpful.

  
    
#
Thanks, Henrik, but 'foo' doesn't do what I want:

x <- "some stuff"
second <- "more stuff"

foo(first=x, second)
[1] "first" ""

Brian's right:
The function using this compares estimates of animal densities, CIs, etc
using different models, with one object containing the results of one model.
It extracts key results and AIC from these objects and does a summary
matrix, with lowest AIC at the top, so the row names need to reflect the
model used.

If the object name is sufficiently explanatory -- eg. point.est.hazardRate
-- then the deparsed argument value is fine as row name. But we need the
option to be more specific if necessary, eg. with "halfNormal=x1,
hazardRate=x2". Just like 'rbind', in fact.

Thanks,  Mike
#
How about

foo <- 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
}

Such things are hard to do from R level, hence the use of match.call to do 
it at C level.
On Fri, 1 Jun 2007, Mike Meredith wrote:

            

  
    
#
On 6/1/07, Mike Meredith <mmeredith at wcs.org> wrote:
Brian's is shorter but I think the one in my post is a bit more robust:
+    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
+ }
+ 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))
+ }
Error in if (!nchar(nm[i])) nm[i] <- deparse(m[[i]]) :
        argument is of length zero
[1] "sin" "cos"
#
Sorry, I responded a bit too hastily last night, without testing the two
functions properly.
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!!
+    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
+ }
+ 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))
+ }
Error in if (!nchar(nm[i])) nm[i] <- deparse(m[[i]]) :
        argument is of length zero
[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.