[R] using a variable name stored in another variable?

Chris Seidel seidel at phaget4.org
Mon Feb 8 00:05:06 CET 2010

Hi Charlie,

get() will return the contents (value) of a variable. But what I want is
to save the named object. Something like save(get(myobjectname), ...)
doesn't work. 

In the environment, is that object of interest, and a variable which
holds the name of the object of interest. If you don't know the name of
the object, but only the variable which contains it's name, how do you
use that information to save the object?

From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On
Behalf Of Sharpie [chuck at sharpsteen.net]
Sent: Sunday, February 07, 2010 4:13 PM
To: r-help at r-project.org
Subject: Re: [R] using a variable name stored in another variable?

Chris Seidel wrote:
> Hello,
> I'm trying to figure out how to create a data object, and then save it
> with a user-defined name that is input as a command line argument. I
> know how to create the object and assign it the new name, however, I
> can't figure out how to refer to the new name for a future operation
> such as save().
> ..snip..

You probably want the get() function:

  get( myobjectname )

The help page for get() has a note which states that it is the compliment of
assign().  Perhaps a similar note should be added to the help page for

Hope this helps!


More information about the R-help mailing list