Saving and loading history automatically
Asked Answered
M

4

9

I'm using the R software for statistical analysis and am sad that it doesn't preserve and restore my prompt command history. Indeed, pressing the up arrow on a newly started interactive R session will reveal a blank history, every time. It would be great if it could do this in manner, say, similar to ipython. I tried putting this in my .Rprofile file to no avail. No file containing my command history is ever created.

.First <- function(){
        if (!any(commandArgs()=='--no-readline') && interactive()){
                require(utils)
                try(loadhistory(Sys.getenv("R_HISTFILE")))
        }
}

.Last <- function() {
        if (!any(commandArgs()=='--no-readline') && interactive()){
                require(utils)
                try(savehistory(Sys.getenv("R_HISTFILE")))
        }
}

Of course this line is present in my .bash_profile

export R_HISTFILE="$HOME/share/r_libs/.history"

All this is happening via SSH on a remote server running Linux. Any help greatly appreciated !

Mccready answered 24/5, 2013 at 12:25 Comment(6)
Not really an answer, but most people I think recommend saving the commands you want to use again in a script. Then you don't have to wade through your entire history to find the few commands you want.Edison
Yeah of course I'll write my final pipeline in a script; but imagine using something like the bash shell without history preservation across sessions, wouldn't you get frustrated too ?Mccready
Not really, no. I type commands I want to keep in my script, and one-offs in the console. Actually, I get frustrated when I forget to put something I want later in my script and have to wade through my history to find it.Edison
You don't mention what UI you're using - maybe try RStudio or similar?Neutral
@Neutral I'm just launching the R prompt from the bash shell after connecting to a node on the cluster with SSH.Mccready
Ah, that clarifies a lot about what kind of solutions are possible and why you care about the history. Does hadley's solution work for you?Edison
N
15

In my ~/.profile I have:

export R_HISTFILE=~/.Rhistory

In my ~/.Rprofile I have:

if (interactive()) {
  .Last <- function() try(savehistory("~/.Rhistory"))
}

and that works for me (although it doesn't work very well if you have multiple R sessions open)

Neutral answered 24/5, 2013 at 15:22 Comment(2)
That doesn't work for me. It will load what ever is in ~/.Rhistory but will not save at the end of each session unless I answer yes to the save workspace image question.Mccready
@Mccready I also have alias R='R --no-save --no-restore-data --quiet' in my profile which eliminates the questions on close.Neutral
C
1

An alternative to setting .Last is registering a finalizer for .GlobalEnv, which will be run even if the R session is exited with EOF (Ctrl+Z on Windows and Ctrl+D elsewhere):

if (interactive()) {
  invisible(
    reg.finalizer(
      .GlobalEnv,
      eval(bquote(function(e) try(savehistory(file.path(.(getwd()), ".Rhistory"))))),
      onexit = TRUE))
}

There are some extra bells and whistles here:

  • invisible() makes sure the return value of reg.finalizer() isn't printed when R starts up
  • Contrary to Hadley's answer, the .Rhistory file is saved in the current directory. eval(bquote(... .(getwd()) ...)) evaluates getwd() during startup, so that the directory that is current during startup is used at exit
  • Setting onexit = TRUE makes sure that the code is actually run
Carrion answered 14/10, 2016 at 19:18 Comment(0)
C
0

If you work with Rgui : savehistory(), loadhistory() and history() could do the job. Otherwise I guess it depends on the IDE..

Collywobbles answered 24/5, 2013 at 14:18 Comment(3)
I'm not interested in saving my session at the moment. I just wanted my history to be preserved. I'm not using any IDE yet.Mccready
First answer was off topic, i'm so tiredCollywobbles
Yes, I have identified the savehistory() and loadhistory() commands. I'm asking how to have this happen automatically at each new session creation.Mccready
E
0

You might consider emacs and ESS, which work fine over SSH and allow for the more usual (and generally considered more powerful) method of keeping useful commands in a separate file.

Edison answered 28/5, 2013 at 18:47 Comment(2)
could you comment more on cons and pros of using ESS and give a tutorial?Tay
That would be a great answer, but unfortunately more than I am up to providing. My best answer is to find a friendly colleague who uses it and go from there. It's different enough from most "modern" programs that having a helping hand in person is especially helpful.Edison

© 2022 - 2024 — McMap. All rights reserved.