[R] Reloading old R Environments/Workspaces

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu Jul 4 19:24:54 CEST 2019


On 04/07/2019 12:32 p.m., Spencer Brackett wrote:
> Hello again,
> 
>    I might be repeating myself here, so my apologies, but do I have to run a
> script file from my R Studio to reimplement my previous work for a given
> project.... so to start up where I left off.... or is opening up R and,
> with my global environment automatically reloading as it was when I last
> worked on, sufficient?


Saving your workspace when you quit is a common default, but it is 
generally a bad idea.  Old junk collects in there, and makes new results 
harder to debug.

A better workflow is to never save the whole workspace.  If you have 
just computed some object(s) and the computation took so long you don't 
want to repeat it, then save just a minimum, and load them later in a 
new session.

A particularly dangerous situation happens if you sometimes save your 
workspace and sometimes don't.  You can end up with situations like this:

Session 1:  compute some random values.  Save the workspace, including 
the random number key.

Session 2:  automatically load the saved workspace.  Compute some new 
random values.  Quit without saving the workspace.

Session 3:  automatically load the saved workspace from Session 1, 
including the random number seed.  Any random values computed in this 
session could be identical to the values in Session 2, because they are 
starting with the same seed.

If you don't have a saved workspace to load, you end up with a blank 
slate, and the random number key is generated based on time of day and 
process number, so is almost certainly different in every session. 
(Sometimes you want a repeated seed for reproducibility, but it's always 
bad when you're surprised by one.)

Duncan Murdoch



More information about the R-help mailing list