[R] retrieve name of an object?

Liviu Andronic landronimirc at gmail.com
Wed Aug 4 16:23:01 CEST 2010


Dear Duncan

On Wed, 04 Aug 2010 08:33:49 -0400
Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
> As other have pointed out, in a function you can use substitute(arg)
> to retrieve the expression passed as arg, and
> deparse(substitute(arg)) to turn it into a string that's suitable for
> using as a label.  But that's not the name of the object.  The name
> of the object in that case is "arg".
> 
Thank you for the explanation. I was indeed trying to retrieve the
expression passed as an argument in a function, to use it as a label.
Sofar the best I can manage is this:
my.fun <- function(x) {
    print(deparse(substitute(x)))
    print(paste(substitute(x)[3]))
}

> my.fun(mtcars$cyl)
[1] "mtcars$cyl"
[1] "cyl"

Thanks all for their suggestions
Liviu



More information about the R-help mailing list