[R] How to save and restore a workspace
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Tue Oct 24 02:46:45 CEST 2017
To be specific, the effect of loading packages with the "library" function is NOT saved when you save the workspace... and if you come back much later after you have updated some packages the old saved objects may not work well with those new packages. At the very least you have to re-load all necessary packages yourself before you load a saved workspace. Many users avoid ever using the "save workspace" option because it can cause old mistakes to come back and haunt you later.
This kind of issue has lead me to become quite systematic about writing R files (as Jim suggested below) that re-do all of my work on a given task (omitting the false starts needed to learn things) on each of my projects starting from a blank workspace. It may seem like a tedious approach to the beginner, but it is actually very comforting to know that I can look through every step and verify that it did what I wanted it to, without wondering how some particular intermediate result was computed six months ago and saved for later use. If one step is particularly slow, I may comment out (our put an "if" around) the statements that originally created and saved that object with saveRDS, and load it instead of recomputing it once I trust it. But the key is to always focus on building sequences of R statements, and later on building functions rather than poking and prodding a workspace like it was a spreadsheet.
--
Sent from my phone. Please excuse my brevity.
On October 23, 2017 3:45:29 PM PDT, Jim Lemon <drjimlemon at gmail.com> wrote:
>Hi Jon,
>Saving your workspace doesn't mean that everything will be rerun when
>you start a new R session. I just means that persistent objects like
>data frames will be there. If you type:
>
>objects()
>
>you will see all of those things that were there when you ended in the
>last session. Things like commands will be in the "history", so you
>can retrieve them just as you did at the end of the last session (up
>arrow). It may be a better solution if you save sequences of commands
>as R script files (e.g. "something.R") as you can run them:
>
>source("something.R")
>
>and edit and save them again.
>
>Jim
>
>On Tue, Oct 24, 2017 at 9:12 AM, <jonnvan at gmail.com> wrote:
>> Hello,
>> I recently downloaded R in hopes of learning to use it for
>statistics.
>> I have promptly run into a problem, as I am unable to save, and later
>recover, a workspace so I can resume work where I left off.
>> I am using Windows.
>> I indicate "yes" to the pop up after q(). Then when I later reopen R
>Console and click on File, I cannot get my prior workspace to appear in
>the R Console frame so I can resume work.
>> In the File drop down menu I have tried Load Workspace, Load History,
>Display file(s)..., opened R Type: R Workspace with no luck.
>> I have read about this in two different books, the R Manual, and R
>FAQs, used the RGui help function, and still cannot do it.
>> I have used Windows for years, but I am ignorant about programming.
>> Would appreciate any help you might offer.
>> I live in the Denver area, so if there are any local resources you
>could direct me to, I would be grateful for that as well.
>>
>> Thank you,
>> Jon VanDeventer
>>
>> Sent from my iPad
>> ______________________________________________
>> 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.
>
>______________________________________________
>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.
More information about the R-help
mailing list