Kasey Kasey - 4 months ago 12
R Question

Recent commands searchable in history but not listed after power outage

I'd been working on a project in RStudio all day when the power went out across half of San Francisco... Once power came back it appeared my commands that weren't from a saved workspace were not longer listed in my command history. HOWEVER those commands are actually searchable (and therefore living somewhere on my machine). I need to save this hidden history to a file so I can run all the commands again. Anyone encountered this problem before?

Answer

EDIT: In contrary to R, RStudio saves all history commands in history_database file, so that history can be retrieved after unexpected RStudio crashes due to power loss, etc..

The history_database file is located in

On Windows: C:\Users\username\AppData\Local\RStudio-Desktop

On Mac | Nix: ~/RStudio-Desktop

history_database file is a normal ASCII text file and it can be viewed by any text editor.

If you can search history in the current workspace, then save all those history commands in a file:

savehistory(file = "myhist.Rhistory")

Then load it whenever necessary

loadhistory(file = "myhist.Rhistory")

To find out the location of history file in the current workspace

getwd()
dir(all.files = TRUE)

Look for ".Rhistory" hidden file. This file contains all history commands.

For more info: ?"history"

If you want to set the default location of the .Rhistory file in your environment,

Get the current value:

options("R_HISTFILE")

Set the R_HISTFILE to a desired location:

options(R_HISTFILE = "file_path")

For more info: ?options

Comments