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

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 1 15:34:27 CEST 2007


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:

>
>
> 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:
>> ...he wants the argument name if there is one otherwise the
>> deparsed argument value, but clarification would be helpful.
>
> 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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list