Rev 27447 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{Startup}\alias{Startup}\alias{Rprofile}\alias{.Rprofile}\alias{Rprofile.site}\alias{Renviron.site}\alias{.Renviron}\alias{.First}\alias{.First.sys}\title{Initialization at Start of an R Session}\usage{.First <- function() { \dots\dots }.Rprofile <startup file>}\description{In \R, the startup mechanism is as follows.Unless \option{--no-environ} was given on the command line, \Rsearches for user and site files to process for setting environmentvariables. The name of the site file is the one pointed to by theenvironment variable \env{R\_ENVIRON}; if this is unset or empty,\file{\$R\_HOME/etc/Renviron.site} is used (if it exists, which itdoes not in a \dQuote{factory-fresh} installation).The user files searched for are \file{.Renviron} in the current or inthe user's home directory (in that order).See \bold{Details} for how the files are read.Then \R searches for the site-wide startup profile unless the commandline option \option{--no-site-file} was given. The name of this fileis taken from the value of the \env{R\_PROFILE} environment variable.If this variable is unset, the default is\file{\$R\_HOME/etc/Rprofile.site}, which is used if it exists(which it does not in a \dQuote{factory-fresh} installation).This code is loaded into package \pkg{base}. Users need to becareful not to unintentionally overwrite objects in base, and itis normally advisable to use \code{\link{local}} if code needs to beexecuted: see the examples.Then, unless \option{--no-init-file} was given, \R searches for a filecalled \file{.Rprofile} in the current directory or in the user'shome directory (in that order) and sources it into the userworkspace.It then loads a saved image of the user workspace from \file{.RData}if there is one (unless \option{--no-restore-data} was specified, or\option{--no-restore}, on the command line).Next, if a function \code{.First} is found on the search path,it is executed as \code{.First()}.Finally, function \code{.First.sys()} in the \pkg{base} package is run.This calls \code{\link{require}} to attach the default packagesspecified by \code{\link{options}("defaultPackages")}.A function \code{.First} (and \code{\link{.Last}}) can be defined inappropriate \file{.Rprofile} or \file{Rprofile.site} files or havebeen saved in \file{.RData}. If you want a different set of packagesthan the default ones when you start, insert a call to\code{\link{options}} in the \file{.Rprofile} or \file{Rprofile.site}file. For example, \code{options(defaultPackages = character())} willattach no extra packages on startup. Alternatively, set\code{R_DEFAULT_PACKAGES=NULL} as an environment variable beforerunning \R. Using \code{options(defaultPackages = "")} or\code{R_DEFAULT_PACKAGES=""} enforces the R \emph{system} default.The commands history is read from the file specified by theenvironment variable \env{R\_HISTFILE} (default \file{.Rhistory})unless \option{--no-restore-history} was specified (or\option{--no-restore}).The command-line flag \option{--vanilla} implies\option{--no-site-file}, \option{--no-init-file},\option{--no-restore} and \option{--no-environ}.}\details{Note that there are two sorts of files used in startup:\emph{environment files} which contain lists of environment variablesto be set, and \emph{profile files} which contain \R code.Lines in a site or user environment file should be either commentlines starting with \code{#}, or lines of the form \code{name=value}.The latter sets the environmental variable \code{name} to\code{value}, overriding an existing value. If \code{value} is of theform \code{${foo-bar}}, the value is that of the environmentalvariable \code{foo} if that exists and is set to a non-empty value,otherwise \code{bar}. This construction can be nested, so\code{bar} can be of the same form (as in \code{${foo-${bar-blah}}}).Leading and trailing white space in \code{value} are stripped.% No other interpretation of \code{value} is performed. In particular,% if it is enclosed in quotes, the quotes form part of the value.\code{value} is processed in a similar way to a Unix shell. In particularquotes are stripped, and backslashes are removed except inside quotes.}#ifdef unix\note{The file \file{\$R\_HOME/etc/Renviron} is always read very early inthe start-up processing. It contains environment variables set by \Rin the configure process. Values in that file can be overriden insite or user environment files: do not change\file{\$R\_HOME/etc/Renviron} itself.}#endif\section{Historical notes}{Prior to \R version 1.4.0, the environment files searched were\file{.Renviron} in the current directory, the file pointed to by\env{R\_ENVIRON} if set, and \file{.Renviron} in the user's homedirectory.Prior to \R version 1.2.1, \file{.Rprofile} was sourced after\file{.RData} was loaded, although the documented order was as here.The format for site and user environment files was changed in version1.2.0. Older files are quite likely to work but may generate warningson startup if they contained unnecessary \code{export} statements.Values in environment files were not processed prior to version1.4.0.}\seealso{\code{\link{.Last}} for final actions before termination.For profiling code, see \code{\link[utils]{Rprof}}.}\examples{\dontrun{# Example ~/.Renviron on UnixR_LIBS=~/R/libraryPAGER=/usr/local/bin/less# Example .Renviron on WindowsR_LIBS=C:/R/libraryMY_TCLTK=yesTCL_LIBRARY=c:/packages/Tcl/lib/tcl8.4# Example of .Rprofileoptions(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")# Example of Rprofile.sitelocal({old <- getOption("defaultPackages")options(defaultPackages = c(old, "MASS"))})## if .Renviron containsFOOBAR="coo\bar"doh\\ex"abc\"def'"## then we get> cat(Sys.getenv("FOOBAR"), "\n")coo\bardoh\exabc"def'}}\keyword{environment}