[R] loading edited functions already in saved workspace automatically

Michael Friendly friendly at yorku.ca
Tue May 9 17:49:22 CEST 2017


Ralf:

You are afflicted with several mind bugs:
* the "my-way mind bug" -- "I want to do it MY WAY, because that's sort 
of what
I know" and also,
* the "my-square-peg-should-fit-into-this-round-hole mind bug" -- "R 
should be able to
do it MY WAY, but it puts obstacles in my path," perhaps a subsidiary, 
but more technical:
* the "loading-a-function-or-data-is-the-same mind bug"
As in many things R, you can't always get to MY WAY from there, at least 
not without a tortuous journey.

You think you should be able to do everything you want in .Rprofile, but 
then you posed two separate problems:
(a) save/reload history
(b) save/reload functions and data

If you recognize them as two separate problems, there is an easier path:
(a) use .Rprofile only for making your history persistent, as I described
(b) Put your functions & data you always want available in a package; 
you can load it from .Rprofile

I originally defined a bunch of handy functions (e.g., cd(), a setwd() 
replacement, that works more like `cd` on unix, in that `cd()` returns 
to the previous directory; it also changes the Windows title to
`RGui:`  abbreviation of getwd() )

I moved them all out of .Rprofile, made a package `myutil` and now load 
them from there with

  #======================
  # load default packages
  #======================
      if (!require(myutil)) warning("myutil functions not available")

hope this helps,
-Michael

On 5/9/2017 10:20 AM, Ralf Goertz wrote:
> 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
>


-- 
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



More information about the R-help mailing list