On 18/06/2014 12:54, Jim Lemon wrote:
On Wed, 18 Jun 2014 04:24:58 PM yzh lin wrote:
Hi, every R user,
I wanna output names of objects, I googled codes from website,
but it
was not what I wanted.
the codes are as following:
f <- function (...)
{
unevaluatedArgs <- substitute(...())
evaluatedArgs <- list(...)
stopifnot(length(unevaluatedArgs) == length(evaluatedArgs))
tags <- vapply(unevaluatedArgs, FUN=function(x) deparse(x)[1],
FUN.VALUE=character(1))
if (!is.null(tmp <- names(evaluatedArgs))) {
# if argument is tagged, tag=expr, use the tag
i <- !is.na(tmp) & tmp != ""
tags[i] <- tmp[i]
}
tags
}
f(a1=log(1:4), m1=1:4, c1=pi)
results:
f(a1=log(1:4), m1=1:4, c1=pi)
a1 m1 c1
"a1" "m1" "c1"
what I wanted is outputting "log(1:4) " "1:4", "pi" , while not "a1" "m1"
"c1" .
Coulde someone tell me how to get the results I wanted?
Hi Yuanzhen,
This may be of some help:
f<-function (...) {
unevaluatedArgs <- unlist(substitute(...()))
tags<-NA
for(arg in 1:length(unevaluatedArgs))
tags[arg]<-deparse(unevaluatedArgs[[arg]])
tags
}
f(a1=log(1:4), m1=1:4, c1=pi)
[1] "log(1:4)" "1:4" "pi"
Or simply
f <- function(...) sapply(subsitute(...()), deparse)
which even keeps the names.
--
Brian D. Ripley, rip...@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
______________________________________________
R-help@r-project.org 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.