save() unable to find object

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

save() unable to find object

Bogaso
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,

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: save() unable to find object

Jim Lemon-4
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
<[hidden email]> 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,
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: save() unable to find object

Bogaso
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 <[hidden email]> 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
> <[hidden email]> 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,
> >
> > ______________________________________________
> > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: save() unable to find object

Ivan Krylov
In reply to this post by Bogaso
On Thu, 31 Oct 2019 14:39:48 +0530
Christofer Bogaso <[hidden email]> wrote:

> Error in save("Date", paste("AAA", format(Date, "%d"), sep = ""), file
> = "Save.RData") :
>   object ‘paste("AAA", format(Date, "%d"), sep = "")’ not found

save() 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: save() unable to find object

Duncan Murdoch-2
In reply to this post by Bogaso
On 31/10/2019 5:17 a.m., Christofer Bogaso wrote:
> As I said the name 'AAA31' is itself a variable. So I cant hard-code
> it within the save() function

Use the list= argument to save().

Duncan Murdoch

>
> On Thu, Oct 31, 2019 at 2:45 PM Jim Lemon <[hidden email]> 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
>> <[hidden email]> 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,
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: save() unable to find object

Bogaso
Thanks Duncan. It worked.

On Thu, Oct 31, 2019 at 3:02 PM Duncan Murdoch <[hidden email]> wrote:

>
> On 31/10/2019 5:17 a.m., Christofer Bogaso wrote:
> > As I said the name 'AAA31' is itself a variable. So I cant hard-code
> > it within the save() function
>
> Use the list= argument to save().
>
> Duncan Murdoch
>
> >
> > On Thu, Oct 31, 2019 at 2:45 PM Jim Lemon <[hidden email]> 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
> >> <[hidden email]> 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,
> >>>
> >>> ______________________________________________
> >>> [hidden email] 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.
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: save() unable to find object

Peter Dalgaard-2
In reply to this post by Bogaso
Try the list= argument

save(list= c("Date", paste(.....)), file=....)

If you use ..., you'll get bitten by

     ...: the names of the objects to be saved (as symbols or character
          strings).

and the paste() construct is neither. (Internally, it gets converted by as.character(substitute(list(...))), leading to the "object...not found" that you see)

The documentation is maybe a little oblique, but the point is that in

bar <- 1234
foo <- "bar"

save(foo) and save("foo") both save the "foo" object, whereas save(list=foo) will save "bar".

We don't evaluate expressions in ... because then save(foo) would also save "bar" (we could  in principle have different behaviour from symbols and more general expressions, but that is a certain road to insanity).

-pd

> On 31 Oct 2019, at 10:17 , Christofer Bogaso <[hidden email]> wrote:
>
> 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 <[hidden email]> 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
>> <[hidden email]> 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,
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.