.Rprofile
is a file containing R code that is executed when you launch R from the directory containing the .Rprofile
file. The similarly named Rprofile.site
, located in R's home directory, is executed by default every time you load R from any directory. Rprofile.site
and to a greater extend .Rprofile
can be used to initialize an R session with personal preferences and various utility functions that you have defined.
Important note: if you use RStudio, you can have a separate
.Rprofile
in every RStudio project directory.
Here are some examples of code that you might include in an .Rprofile file.
# set R_home
Sys.setenv(R_USER="c:/R_home") # just an example directory
# but don't confuse this with the $R_HOME environment variable.
options(papersize="a4")
options(editor="notepad")
options(pager="internal")
options(help_type="html")
.Library.site <- file.path(chartr("\\", "/", R.home()), "site-library")
local({r <- getOption("repos")
r["CRAN"] <- "http://my.local.cran"
options(repos=r)})
This will allow you to not have to install all the packages again with each R version update.
# library location
.libPaths("c:/R_home/Rpackages/win")
Sometimes it is useful to have a shortcut for a long R expression. A common example of this setting an active binding to access the last top-level expression result without having to type out .Last.value
:
makeActiveBinding(".", function(){.Last.value}, .GlobalEnv)
Because .Rprofile is just an R file, it can contain any arbitrary R code.
This is bad practice and should generally be avoided because it separates package loading code from the scripts where those packages are actually used.
See help(Startup)
for all the different startup scripts, and further aspects. In particular, two system-wide Profile
files can be loaded as well. The first, Rprofile
, may contain global settings, the other file Profile.site
may contain local choices the system administrator can make for all users. Both files are found in the ${RHOME}/etc
directory of the R installation. This directory also contains global files Renviron
and Renviron.site
which both can be completemented with a local file ~/.Renviron
in the user's home directory.