[R] How can certain variables be automatically excluded from being saved?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu Apr 1 14:39:45 CEST 2021

On 01/04/2021 4:05 a.m., Ralf Goertz wrote:
> Hi,
> after having read here about the "seed problem" I wonder if there is a
> way to automatically exclude certain variables from being saved when the
> workspace image is saved at the end of an interactive session. I have
> been using .First() and .Last() for ages but apparently they are of no
> help as .First() gets called before loading the workspace and .Last()
> after it has been saved. At least the line
> if (".Random.seed" %in% ls(all.names=T)) rm(.Random.seed, pos=1)
> in either of those functions doesn't have the desired effect.

Jim suggested a way to do that, but I don't think it's really a good 
idea:  it just fixes one aspect of the problem, it doesn't solve the 
whole thing.

The real problem is saving the workspace occasionally, but always 
loading it.  The "always loading" part is automatic, so I think the real 
solution should address the "occasionally saving" part.

If you always save the workspace, things are fine.  You'll save the seed 
at the end of one session, and load it at the beginning of the next.

If you never save the workspace, things are also fine.  You'll always 
generate a new seed in each session that needs one.

Personally, I believe in the "never save it" workflow.  I think it has 
lots of benefits besides the random seed issue:  you won't get a 
more-and-more cluttered workspace over time, you end up with more 
reproducible results, etc.  However, I can understand that some people 
use a different workflow, so "always save it" is sometimes a reasonable 

So the real problem is the "sometimes save it" workflow, which is 
**encouraged** by the default q(save = "default") option, which asks 
when interactive.  Changing the default to act like q(save = "no") would 
be my preference (and that's how I configure things), but changing it to 
act like q(save = "yes") would be an improvement over the current choice.

Duncan Murdoch

More information about the R-help mailing list