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(). The code below creates an object and uses assign() to give it the user supplied name "MyName". However, since I don't know what the new name is in advance, how do I refer to it in the save() command? (the example below only saves an object with the name, not the objec itself). Is it some kind of dereference? Any ideas? command: cat myscript.r | R --vanilla --args MyName script: # get the command-line argument for the variable name myobjectname <- commandArgs()[4] # make some data somedata <- matrix(rnorm(100),10,10) # make a filename for the saved object filename <- paste(myobjectname, ".RData", sep="") # assign data to the new name assign(myobjectname, somedata) # save the object to disk save(myobjectname, file=filename)
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 assign... Hope this helps! -Charlie -- View this message in context: http://n4.nabble.com/using-a-variable-name-stored-in-another-variable-tp1472371p1472400.html Sent from the R help mailing list archive at Nabble.com.
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? -Chris ________________________________________ 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 assign... Hope this helps! -Charlie --
Solved! Duncan Murdoch got it right:> I think you want > > save(list=myobjectname, file= ...) > > assuming that the object has already been created with that name. If > it hasn't, you'll need two steps: > > assign( myobjectname, value) > save(list=myobjectname, file=...)[...] This works great. If you have two objects in your environment, and you only know the name of one, but it contains the name of the other. I had done this because I'm processing Next Gen Sequencing data files which are several gigabytes in size, but can be reduced to an object of several megabytes, and saved to disk for analysis later with less memory. This allows me to write a script which takes command line arguments, including a name for the object I create and save. I had looked in the R FAQ: varname <- c("a", "b", "d") eval(substitute(lm(y ~ x + variable), list(variable = as.name(varname[1])))) but between as.name, eval, and substitute, I was totally confused and couldn?t get it right. -Chris> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Thomas Lumley > Sent: Monday, February 08, 2010 10:09 AM > To: Sharpie > Cc: r-help at r-project.org > Subject: Re: [R] using a variable name stored in another variable? > > On Sun, 7 Feb 2010, Sharpie wrote: > > > 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 assign... > > It's also FAQ 7.21. Not quite as famous as 7.31, but still a > good vintage. > > > -thomas > > Thomas Lumley Assoc. Professor, Biostatistics > tlumley at u.washington.edu University of Washington, Seattle