Ralf Goertz
2017-May-05 08:33 UTC
[R] loading edited functions already in saved workspace automatically
Hi, In short: Is it possible to have the previously saved workspace restored and nevertheless load a function already existing in this workspace via .Rprofile anyway? In detail: I use different directories for different projects. In all those projects I use a function which I therefore try to get into the session by `myfunc=eval(parse(file=("~/R/myfunc.R")))' in ~/.Rprofile. Once I leave the session thereby saving the workspace this function gets saved in ./.RData as well. In a subsequent session in that directory it gets loaded back. However, in the meantime I might have edited ~/R/myfunc.R. I don't seem to be able to automatically load the new function into the session. The workspace gets loaded *after* the execution of ~/.Rprofile. So the new definition of myfunc() gets overwritten by the old one. I can't use .First() ? which is executed after loading the workspace ? because this would load myfunc() into the environment of .First() instead of the global environment. I could use .Last() to remove the function before saving the workspace. But then .Last() gets saved to the workspace which is also not convenient since when I add another function the same way and edit the definition of .Last() in ~/.Rprofile to also remove that function this does not work because I don't get the new .Last() into the session automatically. And no, removing .Last() from within .Last() doesn't work.
Jeff Newmiller
2017-May-05 13:30 UTC
[R] loading edited functions already in saved workspace automatically
The answer most people seem to use is to avoid depending on functions in RData files, and in particular avoiding ever saving the "automatic" ".RData" files at all. (Some people avoid using any RData files, but the automatic loading of functions by ".RData" files is a particularly pernicious source of evil as you have already discovered.) That is, always work toward building scripts that you run to restore your workspace rather than depending on save files. Don't depend on save files to keep track of what you do interactively. This also usually means that there should be little if anything in your .Rprofile because that tends to build non-reproducibility into your scripts. -- Sent from my phone. Please excuse my brevity. On May 5, 2017 1:33:27 AM PDT, Ralf Goertz <r_goertz at web.de> wrote:>Hi, > >In short: Is it possible to have the previously saved workspace >restored >and nevertheless load a function already existing in this workspace via >.Rprofile anyway? > >In detail: I use different directories for different projects. In all >those projects I use a function which I therefore try to get into the >session by `myfunc=eval(parse(file=("~/R/myfunc.R")))' in ~/.Rprofile. >Once I leave the session thereby saving the workspace this function >gets >saved in ./.RData as well. In a subsequent session in that directory it >gets loaded back. However, in the meantime I might have edited >~/R/myfunc.R. I don't seem to be able to automatically load the new >function into the session. The workspace gets loaded *after* the >execution of ~/.Rprofile. So the new definition of myfunc() gets >overwritten by the old one. I can't use .First() ? which is executed >after loading the workspace ? because this would load myfunc() into the >environment of .First() instead of the global environment. I could use >.Last() to remove the function before saving the workspace. But then >.Last() gets saved to the workspace which is also not convenient since >when I add another function the same way and edit the definition of >.Last() in ~/.Rprofile to also remove that function this does not work >because I don't get the new .Last() into the session automatically. And >no, removing .Last() from within .Last() doesn't work. > >______________________________________________ >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.
Ralf Goertz
2017-May-05 13:44 UTC
[R] loading edited functions already in saved workspace automatically
Am Fri, 05 May 2017 06:30:01 -0700 schrieb Jeff Newmiller <jdnewmil at dcn.davis.ca.us>:> The answer most people seem to use is to avoid depending on functions > in RData files, and in particular avoiding ever saving the > "automatic" ".RData" files at all. (Some people avoid using any RData > files, but the automatic loading of functions by ".RData" files is a > particularly pernicious source of evil as you have already > discovered.) > > That is, always work toward building scripts that you run to restore > your workspace rather than depending on save files. Don't depend on > save files to keep track of what you do interactively. This also > usually means that there should be little if anything in > your .Rprofile because that tends to build non-reproducibility into > your scripts.Hi Jeff, thanks for your answer. Actually, I don't use the workspace saving feature primarily for the data but for the command line history. Is there a way to just save .Rhistory? Ralf