How to Save the History of Your Work in R
By default, R keeps track of all the commands you use in a session. This tracking can come in handy if you need to reuse a command you used earlier or want to keep track of the work you did before. These previously used commands are kept in the history.
You can browse the history from the command line by pressing the up-arrow and down-arrow keys. When you press the up-arrow key, you get the commands you typed earlier at the command line. You can press Enter at any time to run the command that is currently displayed.
Saving the history is done using the savehistory() function. By default, R saves the history in a file called .Rhistory in your current working directory. This file is automatically loaded again the next time you start R, so you have the history of your previous session available.
If you want to use another filename, use the argument file like this:
> savehistory(file = "Chapter3.Rhistory")
Be sure to add the quotation marks around the filename.
You can open an Explorer window and take a look at the history by opening the file in a normal text editor, like Notepad.
You don’t need to use the file extension .Rhistory — R doesn’t care about extensions that much. But using .Rhistory as a file extension will make it easier to recognize as a history file.
If you want to load a history file you saved earlier, you can use the loadhistory() function. This will replace the history with the one saved in the .Rhistory file in the current working directory. If you want to load the history from a specific file, you use the file argument again, like this: