Andreas
Andreas

Reputation: 7550

How to append to R history file instead of overwriting?

I've put this in my .Rprofile:

Sys.setenv(R_HISTSIZE='1000000')
Sys.setenv(R_HISTFILE='~/.Rhistory')

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

After quitting an R session, the history is written to the file, but the file is overwritten! How can I make R append to the history file instead?

Upvotes: 5

Views: 757

Answers (1)

Spacedman
Spacedman

Reputation: 94202

After saving the history file you could use R's file read-write commands to append that history file to a different file, called whatever you like (~/.R-ancient-history maybe?).

And R has a function for that! Try

file.append("~/.R-ancient-history","~/.Rhistory")

in your .Last.

If you really want it all in .Rhistory, then do file.rename("~/.R-ancient-history","~/.Rhistory") after the append, which should do it...

All pretty well untested... That's your job!

Upvotes: 2

Related Questions