Startup {base} | R Documentation |
In R, the startup mechanism is as follows.
Unless --no-environ
was given, R searches for user and site
files to process for setting environment variables. The name of the
site file is the one pointed to by the environment variable
R\_ENVIRON
; if this is unset,
‘\$R\_HOME/etc/Renviron.site’
is used. The user files searched for are ‘.Renviron’ in the
current or in the user's home directory (in that order).
See Details for how the files are read.
Then R searches for the site-wide startup profile unless the command
line option --no-site-file
was given. The name of this file is
taken from the value of the R\_PROFILE
environment variable.
If this variable is unset, the default is
‘\$R\_HOME/etc/Rprofile.site’.
This code is loaded into package base
.
Then, unless --no-init-file
was given, R searches for a file
called ‘.Rprofile’ in the current directory or in the user's
home directory (in that order) and sources it into the user
workspace.
It then loads a saved image of the user workspace from ‘.RData’
if there is one (unless --no-restore-data
was specified, or
--no-restore
).
Finally, if a function .First
exists, it is executed as
.First()
.
The functions .First
and .Last
can be defined
in the appropriate startup profiles or reside in ‘.RData’.
The commands history is read from the file specified by the
environment variable R_HISTFILE
(default .Rhistory
)
unless --no-restore-history
was specified (or
--no-restore
).
The command-line flag --vanilla
implies --no-init-file
,
--no-restore
and --no-environ
.
.First <- function() { ...... }
.Rprofile <startup file>
Lines in a site or user environment file should be either comment
lines starting with #
, or lines of the form name=value
.
The latter sets the environmental variable name
to
value
, overriding an existing value. If value
is of the
form ${foo-bar}
, the value is that of the environmental
variable foo
if that exists and is set to a non-empty value,
otherwise bar
. This construction can be nested, so
bar
can be of the same form (as in ${foo-${bar-blah}}
).
Leading and trailing white space in value
are stripped.
value
is processed in a similar to a Unix shell. In particular
quotes are stripped, and backslashes are removed except inside quotes.
Prior to R version 1.4.0, the environment files searched were
‘.Renviron’ in the current directory, the file pointed to by
R_ENVIRON
if set, and ‘.Renviron’ in the user's home
directory.
Prior to R version 1.2.1, ‘.Rprofile’ was sourced after ‘.RData’ was loaded, although the documented order was as here.
The format for site and user environment files was changed in version
1.2.0. Older files are quite likely to work but may generate warnings
on startup if they contained unnecessary export
statements.
Values in environment files were not processed prior to version 1.4.0.
.Last
for final actions before termination.
## Not run: ## Some examples with a Unix flavour
# ~/.Renviron
R_LIBS=~/R/library
PAGER=/usr/local/bin/less
# .Rprofile
options(width=65, digits=5)
options(show.signif.stars=FALSE)
ps.options(horizontal=FALSE)
set.seed(1234)
.First <- function() cat("\n Welcome to R!\n\n")
.Last <- function() cat("\n Goodbye!\n\n")
## if .Renviron contains
FOOBAR="coo\bar"doh\ex"abc\"def'"
## then we get
> cat(Sys.getenv("FOOBAR"), "\n")
coo\bardoh\exabc"def'
## End(Not run)