Like that of many sophisticated software applications, R’s behavior can be customized using startup files. In addition, R can save all or part of a session, such as a record of what you did, to an output file. If there are R commands that you would like to execute at the beginning of every R session, you can place them in a file called .Rprofile located either in your home directory or in the directory from which you are running R. The latter directory is searched for this file first, which allows you to have custom profiles for particular projects.
For example, to set the text editor that R invokes if you call edit()
, you can use a line in .Rprofile like this (if you’re on a Linux system):
options(editor="/usr/bin/vim")
R’s options()
function is used for configuration, that is, to tweak various settings. You can specify the full path to your own editor, using the notation (slashes or backslashes) appropriate to your operating system.
As another example, in .Rprofile on my Linux machine at home, I have the following line:
.libPaths("/home/nm/R")
This automatically adds a directory that contains all my auxiliary packages to my R search path.
Like most programs, R has the notion of your current working directory. Upon startup, this will be the directory from which you launched R, if you’re using Linux or a Mac. In Windows, it will probably be your Documents folder. If you then reference files during your R session, they will be assumed to be in that directory. You can always check your current directory by typing the following:
> getwd()
You can change your working directory by calling setwd()
with the desired directory as a quoted argument. For example,
> setwd("q")
would set the working directory to q.
As you proceed through an interactive R session, R records the commands you submit. If you answer yes to the question “Save workspace image?” when you quit, R will save all the objects you created in that session and restore them in your next session. This means you do not need to redo the work from scratch to continue where you left off.
The saved workspace is in a file named .Rdata, which is located either in the directory from which you invoked the R session (Linux) or in the R installation directory (Windows). You can consult the .Rhistory file, which records your commands, to remind yourself how that workspace was created.
If you want speedier startup/shutdown, you can skip loading all those files and the saving of your session at the end by running R with the vanilla
option:
R --vanilla
Other options fall between vanilla and “load everything.” You can find more information about startup files by querying R’s online help facility, as follows:
> ?Startup