Hi, I wanted to save a few R objects in RData file for some future use. The names of such R objects are actually dynamic so I used below code to save them - Date = Sys.Date() assign(paste('AAA', format(Date, "%d"), sep = ""), 5) save('Date', paste('AAA', format(Date, "%d"), sep = ""), file = 'Save.RData') With this, I am getting below error - Error in save("Date", paste("AAA", format(Date, "%d"), sep = ""), file = "Save.RData") : object ?paste("AAA", format(Date, "%d"), sep = "")? not found But I have the object in the workplace -> AAA31[1] 5 I will really appreciate if someone can point towards the right direction. Thanks,
Hi Christofer, This is a guess, but have you tried: save(AAA31,file="Save.RData") Jim On Thu, Oct 31, 2019 at 8:10 PM Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> > Hi, > > I wanted to save a few R objects in RData file for some future use. > The names of such R objects are actually dynamic so I used below code > to save them - > > Date = Sys.Date() > assign(paste('AAA', format(Date, "%d"), sep = ""), 5) > save('Date', paste('AAA', format(Date, "%d"), sep = ""), file = 'Save.RData') > > With this, I am getting below error - > > Error in save("Date", paste("AAA", format(Date, "%d"), sep = ""), file > = "Save.RData") : > object ?paste("AAA", format(Date, "%d"), sep = "")? not found > > But I have the object in the workplace - > > > AAA31 > [1] 5 > > I will really appreciate if someone can point towards the right direction. > > Thanks, > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
As I said the name 'AAA31' is itself a variable. So I cant hard-code it within the save() function On Thu, Oct 31, 2019 at 2:45 PM Jim Lemon <drjimlemon at gmail.com> wrote:> > Hi Christofer, > This is a guess, but have you tried: > > save(AAA31,file="Save.RData") > > Jim > > On Thu, Oct 31, 2019 at 8:10 PM Christofer Bogaso > <bogaso.christofer at gmail.com> wrote: > > > > Hi, > > > > I wanted to save a few R objects in RData file for some future use. > > The names of such R objects are actually dynamic so I used below code > > to save them - > > > > Date = Sys.Date() > > assign(paste('AAA', format(Date, "%d"), sep = ""), 5) > > save('Date', paste('AAA', format(Date, "%d"), sep = ""), file = 'Save.RData') > > > > With this, I am getting below error - > > > > Error in save("Date", paste("AAA", format(Date, "%d"), sep = ""), file > > = "Save.RData") : > > object ?paste("AAA", format(Date, "%d"), sep = "")? not found > > > > But I have the object in the workplace - > > > > > AAA31 > > [1] 5 > > > > I will really appreciate if someone can point towards the right direction. > > > > Thanks, > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code.
On Thu, 31 Oct 2019 14:39:48 +0530 Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Error in save("Date", paste("AAA", format(Date, "%d"), sep = ""), file > = "Save.RData") : > object ?paste("AAA", format(Date, "%d"), sep = "")? not foundsave() uses non-standard evaluation [*], which means that, instead of just getting parameter values, it gets the expressions supplied to it by the caller in their original form. This makes it possible to pass variable names to save() unquoted: save() will still get the name, not the value of the variable. This also causes error messages like yours. To prevent the non-standard evaluation from causing problems, pass the names of the variables as a character vector in the list = ... argument to save(). Alternatively, consider avoiding "variable variable names". Restricting objects with programmatically generated names to a named list might lead to cleaner code: mydata <- list() mydata[paste('AAA', format(Date, "%d"), sep = ""] <- 5 saveRDS(mydata, 'mydata.rds') -- Best regards, Ivan [*] https://adv-r.hadley.nz/metaprogramming.html