Ralf Goertz
2017-May-05 14:23 UTC
[R] loading edited functions already in saved workspace automatically
Am Fri, 05 May 2017 07:14:36 -0700 schrieb Jeff Newmiller <jdnewmil at dcn.davis.ca.us>:> R normally prompts you to save .RData, but it just automatically > saves .Rhistory... the two are unrelated.Not here. If I say "n" to the prompted question "Save workspace image? [y/n/c]: " my history doesn't get saved. Version: R version 3.3.1 (2016-06-21) -- "Bug in Your Hair" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64-suse-linux-gnu (64-bit)
Jeff Newmiller
2017-May-05 14:36 UTC
[R] loading edited functions already in saved workspace automatically
Read ?history. Seems somewhat platform dependent, but they ARE different. -- Sent from my phone. Please excuse my brevity. On May 5, 2017 7:23:14 AM PDT, Ralf Goertz <r_goertz at web.de> wrote:>Am Fri, 05 May 2017 07:14:36 -0700 >schrieb Jeff Newmiller <jdnewmil at dcn.davis.ca.us>: > >> R normally prompts you to save .RData, but it just automatically >> saves .Rhistory... the two are unrelated. > >Not here. If I say "n" to the prompted question "Save workspace image? >[y/n/c]: " my history doesn't get saved. > >Version: > >R version 3.3.1 (2016-06-21) -- "Bug in Your Hair" >Copyright (C) 2016 The R Foundation for Statistical Computing >Platform: x86_64-suse-linux-gnu (64-bit)
Michael Friendly
2017-May-06 15:17 UTC
[R] loading edited functions already in saved workspace automatically
On 5/5/2017 10:23 AM, Ralf Goertz wrote:> Am Fri, 05 May 2017 07:14:36 -0700 > schrieb Jeff Newmiller <jdnewmil at dcn.davis.ca.us>: > >> R normally prompts you to save .RData, but it just automatically >> saves .Rhistory... the two are unrelated. > > Not here. If I say "n" to the prompted question "Save workspace image? > [y/n/c]: " my history doesn't get saved. > > Version: > > R version 3.3.1 (2016-06-21) -- "Bug in Your Hair" > Copyright (C) 2016 The R Foundation for Statistical Computing > Platform: x86_64-suse-linux-gnu (64-bit) >On Windoze, here's what I use in my .Rprofile, which runs every time I start an RGUI coonsole. The key is .First & .Last to load/save history automagically. There is some extra overhead in my use of old.packages() here, but that is for a different reason. ### History # could use Sys.getenv("R_HISTFILE") # Sys.getenv("R_HISTSIZE"=1024) .First <- function() { if(interactive()) { if (.Platform$GUI == "Rgui") { histfile <- if (file.exists(".Rhistory")) ".Rhistory" else "c:/R/.Rhistory" try(utils::loadhistory(histfile)) old <- utils::old.packages() if (! is.null(old)) cat("Updatable packages: ", old[,1], "\n", fill=TRUE) else cat("All packages up to date\n") } setwd("c:/R") cat(paste("[.Rprofile loaded, current dir:", getwd(), "]\n"),sep=" ") } } .Last <- function() if(interactive() && .Platform$GUI == "Rgui") { histfile <- if (file.exists(".Rhistory")) ".Rhistory" else "c:/R/.Rhistory" try(utils::savehistory(histfile)) } -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. & Chair, Quantitative Methods York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA
Ralf Goertz
2017-May-09 14:20 UTC
[R] loading edited functions already in saved workspace automatically
Am Sat, 6 May 2017 11:17:42 -0400 schrieb Michael Friendly <friendly at yorku.ca>:> On 5/5/2017 10:23 AM, Ralf Goertz wrote: > > Am Fri, 05 May 2017 07:14:36 -0700 > > schrieb Jeff Newmiller <jdnewmil at dcn.davis.ca.us>: > > > >> R normally prompts you to save .RData, but it just automatically > >> saves .Rhistory... the two are unrelated. > > > > Not here. If I say "n" to the prompted question "Save workspace > > image? [y/n/c]: " my history doesn't get saved. > > > > Version: > > > > R version 3.3.1 (2016-06-21) -- "Bug in Your Hair" > > Copyright (C) 2016 The R Foundation for Statistical Computing > > Platform: x86_64-suse-linux-gnu (64-bit) > > > > On Windoze, here's what I use in my .Rprofile, which runs every time > I start an RGUI coonsole. The key is .First & .Last to load/save > history automagically.Hi Michael, thanks. This helps with saving the history without saving the data. But actually I'd really like to save both and still be able to load functions automatically from .Rprofile. Not saving the data as Jeff suggested is not a good option because it is sometimes tedious to rebuild my environment by reexecuting commands in the history. And I explained in my OP why I can't use .First() to achieve my goal. But let me try again to explain the problem because I think not everybody understood what I was trying to say. For simplicity I use the plain variable "a" instead of a function. Start a fresh session and remove all variables, define one variable and quit with saving:> rm(list=ls()) > a=17 > quit(save="yes")Now, before opening a new session edit .Rprofile such that it contains just the two lines: print("Hello from .Rprofile") a=42 Start a new session where your saved environment will be loaded. Observe that you see the line [1] "Hello from .Rprofile" proving that the commands in .Rprofile have been executed. Now look at "a":> a[1] 17 You would expect to see this because *after* your "Hello" line you find [Previously saved workspace restored] So you have set "a" to 42 in .Rprofile but it gets overwritten from the previously saved and now restored workspace. On the other hand, .First() gets executed after the restoring of the workspace. Therefore, I could edit .Rprofile to read .First=function(){ assign("a",42,pos=1) } Now, after starting I see that "a" is indeed 42. But then it turns out that from now on I need "a" to be 11. After editing .Rprofile accordingly, I am quite hopeful but after starting a new session I see that "a" is still 42. Why is that? Because .First() was saved and when I started a new session it got a new function body (setting "a" to 11) but before it could be executed it was again overwritten by the old value (setting "a" to 42) and I am chasing my own tail. Sigh. .Last() doesn't help. Apparently (at least on my linux system) it is executed *after* saving the environment so too late to remove anything you don't want saved. In that regard linux doesn't seem to be typical, since in "?.Last" the reverse order is described as typical: Exactly what happens at termination of an R session depends on the platform and GUI interface in use. A typical sequence is to run ?.Last()? and ?.Last.sys()? (unless ?runLast? is false), to save the workspace if requested (and in most cases also to save the session history: see ?savehistory?), then run any finalizers (see ?reg.finalizer?) that have been set to be run on exit, close all open graphics devices, remove the session temporary directory and print any remaining warnings (e.g., from ?.Last()? and device closure). IMHO this is a design flaw. Ralf