Ralf Goertz
2021-Apr-01 08:05 UTC
[R] How can certain variables be automatically excluded from being saved?
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 Lemon
2021-Apr-01 10:20 UTC
[R] How can certain variables be automatically excluded from being saved?
Hi Ralph, I suppose you could write a wrapper for q(): byebye<-function(drop=".Random.seed",save = "default", status = 0, runLast = TRUE) { if(drop %in% ls(all.names=TRUE)) rm(drop,pos=1) q(save=save,status=status,runLast=runLast) ) warning: untested Jim On Thu, Apr 1, 2021 at 7:05 PM Ralf Goertz <r_goertz at web.de> 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. > > ______________________________________________ > 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.
Duncan Murdoch
2021-Apr-01 12:39 UTC
[R] How can certain variables be automatically excluded from being saved?
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 choice. 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