Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I increase the length of the command history in R?

Tags:

r

In R, I like to use reverse search (ctrl+r) to redo infrequent but complex commands without a script. Frequently, I will do so many other commands in between that the command history discards the old command. How can I change the default length of the command history?

like image 813
John Doucette Avatar asked Sep 08 '11 20:09

John Doucette


People also ask

How do I get command history in R?

If you are using RStudio on top of your R distro, CTRL + UP will give you the list of all previous commands.

How do I save my history on RStudio?

You can save the output in the history window by clicking on the save button using a *. r extension on your file so that the file can be automatically opened in R. (You can similarly have a written record of your commands by using the script window and saving a file with the commands that you created.)

How do I view history in RStudio?

1 The History Tab. In the upper right pane of R Studio, you'll see the Environment and History pane. Click on the History tab, and you should see a history of all of the commands that you have sent to the R console in this session. Figure 2.8: Notice the code in the History tab.


1 Answers

This is platform and console specific. From the help for ?savehistory:

There are several history mechanisms available for the different R consoles, which work in similar but not identical ways...

...

The history mechanism is controlled by two environment variables: R_HISTSIZE controls the number of lines that are saved (default 512), and R_HISTFILE sets the filename used for the loading/saving of history if requested at the beginning/end of a session (but not the default for these functions). There is no limit on the number of lines of history retained during a session, so setting R_HISTSIZE to a large value has no penalty unless a large file is actually generated.

So, in theory, you can read and set R_HISTSIZE with:

Sys.getenv("R_HISTSIZE")
Sys.setenv(R_HISTSIZE = new_number)

But, in practise, this may or may not have any effect.

See also ?Sys.setenv and ?EnvVar

like image 98
Andrie Avatar answered Oct 20 '22 06:10

Andrie