[R] how to convert variable name to string?

Jinsong Zhao jszhao at yeah.net
Tue Aug 14 16:54:02 CEST 2012


Hi there,

In the following function:

demo.plot <- function( obj ) plot(1:5, obj, xlab = "x")

Then, I use this function as following:

y <- rnorm(5)
demo.plot(y)

It will produce a plot with ylab = "obj", however, I hope to get a plot 
with ylab = "y".

Is it possible to convert object/variable name y to a string "y", so I 
can set it in demo.plot():

demo.plot <- function( obj ){
    lab.y <- obj.name.2.string(obj) #### I need obj.name.2.string()
    plot(1:5, obj, xlab = "x", ylab = lab.y)
}

I don't know how to define the function obj.name.2.string.

Any suggestion will be greatly appreciated.

Regards,
Jinsong



More information about the R-help mailing list