Julio Sergio Santana
2015-Nov-13 02:41 UTC
[R] Problems using save to store NAMED R objects
I have to store (in a file) an R object that was created with is name as a string of characters, as follows: : nn <- "xxx" : assign(nn, 5) : xxx [1] 5 I don't want to store the object nn but the object xxx. I tried the following two expressions but none of them worked: : save(get(nn), file="f.RData") Error in save(get(nn), file = "f.RData") : object ?get(nn)? not found : save(eval(nn), file="f.RData") Error in save(eval(nn), file = "f.RData") : object ?eval(nn)? not found I know that both save(xxx, ..), and save("xxx", ..) work, but the fact is that the string is going to be provided by an user: : nn <- readline("Your variable->") and : save(nn, file="f.RData") stores the objet nn not xxx Do you have any comments on this? Thanks -Sergio.
On 12/11/2015 6:41 PM, Julio Sergio Santana wrote:> I have to store (in a file) an R object that was created with is name as > a string of characters, as follows: > > : nn <- "xxx" > : assign(nn, 5) > : xxx > [1] 5 > > I don't want to store the object nn but the object xxx. I tried the > following two expressions but none of them worked: > > : save(get(nn), file="f.RData") > Error in save(get(nn), file = "f.RData") : object ?get(nn)? not found > > : save(eval(nn), file="f.RData") > Error in save(eval(nn), file = "f.RData") : object ?eval(nn)? not > found > > I know that both save(xxx, ..), and save("xxx", ..) work, but the fact > is that the string is going to be provided by an user: > > : nn <- readline("Your variable->") > > and > : save(nn, file="f.RData") > stores the objet nn not xxx > > Do you have any comments on this?I believe do.call(save, list(as.name(nn), file = "f.RData")) should do what you want. There are probably other ways, maybe simpler ones. If you're interested, the theory here is that do.call() constructs a call to the first argument, with arguments found by evaluating the second argument. Duncan Murdoch
Julio Sergio Santana
2015-Nov-13 03:14 UTC
[R] Problems using save to store NAMED R objects
Thanks Duncan, I just saw another, but similar, solution for this in http://stackoverflow.com/questions/11084395/save-object-using-variable-with-object-name , and it is: : save(list=nn, file="f.RData") Thanks again, -Sergio. On Thu, Nov 12, 2015 at 8:57 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 12/11/2015 6:41 PM, Julio Sergio Santana wrote: > >> I have to store (in a file) an R object that was created with is name as >> a string of characters, as follows: >> >> : nn <- "xxx" >> : assign(nn, 5) >> : xxx >> [1] 5 >> >> I don't want to store the object nn but the object xxx. I tried the >> following two expressions but none of them worked: >> >> : save(get(nn), file="f.RData") >> Error in save(get(nn), file = "f.RData") : object ?get(nn)? not found >> >> : save(eval(nn), file="f.RData") >> Error in save(eval(nn), file = "f.RData") : object ?eval(nn)? not >> found >> >> I know that both save(xxx, ..), and save("xxx", ..) work, but the fact >> is that the string is going to be provided by an user: >> >> : nn <- readline("Your variable->") >> >> and >> : save(nn, file="f.RData") >> stores the objet nn not xxx >> >> Do you have any comments on this? >> > > I believe > > do.call(save, list(as.name(nn), file = "f.RData")) > > should do what you want. There are probably other ways, maybe simpler > ones. > > If you're interested, the theory here is that do.call() constructs a call > to the first argument, with arguments found by evaluating the second > argument. > > Duncan Murdoch >-- [image: Julio Sergio Santana on about.me] Julio Sergio Santana about.me/juliosergio00 <http://about.me/juliosergio00> Julio Sergio Santana Instituto Mexicano de Tecnolog?a del Agua Tel +52 777 3293600 x 826 Tel/Fax +52 777 3293683 ------------------------------------------- [[alternative HTML version deleted]]