[R] How to output name of object

Jim Lemon jim at bitwrit.com.au
Wed Jun 18 13:54:26 CEST 2014


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"      

Jim



More information about the R-help mailing list