Rev 47259 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/Startup.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{Startup}\Rdversion{1.1}\alias{Startup}\alias{Rprofile}\alias{.Rprofile}\alias{Rprofile.site}\alias{Renviron}\alias{Renviron.site}\alias{.Renviron}\alias{.First}\alias{.First.sys}\alias{.OptRequireMethods}\concept{environment variable}\alias{R_DEFAULT_PACKAGES}\alias{R_ENVIRON}\alias{R_ENVIRON_USER}\alias{R_PROFILE}\alias{R_PROFILE_USER}\title{Initialization at Start of an R Session}\usage{.First <- function() { \dots\dots }\special{.Rprofile <startup file>}}\description{In \R, the startup mechanism is as follows.Unless \option{--no-environ} was given on the command line, \Rsearches for site and user 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{\var{\link[base:Rhome]{R_HOME}}/etc/Renviron.site} is used (if it exists, which itdoes not in a \sQuote{factory-fresh} installation).The name of the user file can be specified by the \env{R_ENVIRON_USER}environment variable; if this is unset, the user files searched for are\file{.Renviron} in the current or in the user's home directory (inthat order).#ifdef windows(See \sQuote{See also} for how the home directory is found on Windows.)#endifSee \sQuote{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{\var{\link[base:Rhome]{R_HOME}}/etc/Rprofile.site}, which is used if it exists#ifdef unix(which it does not in a \sQuote{factory-fresh} installation).#endif#ifdef windows(it contains settings from the installer in a \sQuote{factory-fresh}installation).#endifThis code is sourced into the \pkg{base} package. Users need to becareful not to unintentionally overwrite objects in \pkg{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 userprofile file. The name of this file can be specified by the\env{R_PROFILE_USER} environment variable. If this is unset, a filecalled \file{.Rprofile} in the current directory or in the user'shome directory (in that order) is searched for. The user profile issourced into the user workspace.Note that when the site and user profile files are sourced only the\pkg{base} package is loaded, so objects in other packages need to bereferred to by e.g. \code{utils::dump.frames} or after explicitlyloading the package concerned.It then loads a saved image of the user workspace from \file{.RData}if there is one (unless \option{--no-restore-data} or\option{--no-restore} was specified 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 packages specified by\code{\link{options}("defaultPackages")}. If the \pkg{methods}package is included, this will have been attached earlier (by function\code{.OptRequireMethods()}) so that name space initializations suchas those from the user workspace will proceed correctly.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 (only the \code{base} package) (orset \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.On front-ends which support it, the commands history is read from thefile specified by the environment 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}.#ifdef windowsUnder Windows, it also implies \option{--no-Rconsole}, whichprevents loading the \file{\link{Rconsole}} file.#endif}\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{\var{name}=\var{value}}. The latter sets the environmentalvariable \code{\var{name}} to \code{\var{value}}, overriding anexisting value. If \code{\var{value}} contains an expression 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}. (If it is of the form \code{${foo}}, thedefault is \code{""}.) This construction can be nested, so \code{bar}can be of the same form (as in \code{${foo-${bar-blah}}}). Note thatthe braces are essential: \code{$HOME} will not be interpreted.Leading and trailing white space in \code{\var{value}} are stripped.\code{\var{value}} is then processed in a similar way to a Unix shell:in particular the outermost level of (single or double) quotes isstripped, and backslashes are removed except inside quotes.}\note{#ifdef unixThe file \file{\var{\link[base:Rhome]{R_HOME}}/etc/Renviron} isalways read very early in the start-up processing. It containsenvironment variables set by \R in the configure process. Values inthat file can be overridden in site or user environment files: do notchange \file{\var{\link[base:Rhome]{R_HOME}}/etc/Renviron} itself.Note that this is distinct from\file{\var{\link[base:Rhome]{R_HOME}}/etc/Renviron.site}.#endif#ifdef windowsUnix versions of \R have a file\file{\var{\link[base:Rhome]{R_HOME}}/etc/Renviron}which is read very early in the start-up processing. It containsenvironment variables set by \R in the configure process, and is notused on \R for Windows.#endif}\seealso{#ifdef windowsFor the definition of the home directory see the rw-FAQ. It can befound from a running \R by \code{Sys.getenv("R_USER")}.#endif\code{\link{.Last}} for final actions at the close of an \R session.\code{\link{commandArgs}} for accessing the command line arguments.There are examples of using startup files to set defaults for graphicsdevices in the help for#ifdef windows\code{\link{windows.options}}.#endif#ifdef unix\code{\link{X11}} and \code{\link{quartz}}.#endif\emph{An Introduction to R} for more command-line options: thoseaffecting memory management are covered in the help file for\link{Memory}.For profiling code, see \code{\link{Rprof}}.}\examples{\dontrun{## Example ~/.Renviron on UnixR_LIBS=~/R/libraryPAGER=/usr/local/bin/less## Example .Renviron on WindowsR_LIBS=C:/R/libraryMY_TCLTK="c:/Program Files/Tcl/bin"## Example of setting R_DEFAULT_PACKAGES (from R CMD check)R_DEFAULT_PACKAGES='utils,grDevices,graphics,stats'# this loads the packages in the order given, so they appear on# the search path in reverse order.## Example of .Rprofileoptions(width=65, digits=5)options(show.signif.stars=FALSE)setHook(packageEvent("grDevices", "onLoad"),function(...) grDevices::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({# add MASS to the default packages, set a CRAN mirrorold <- getOption("defaultPackages"); r <- getOption("repos")r["CRAN"] <- "http://my.local.cran"options(defaultPackages = c(old, "MASS"), repos = r)})## if .Renviron containsFOOBAR="coo\bar"doh\\ex"abc\"def'"## then we get# > cat(Sys.getenv("FOOBAR"), "\n")# coo\bardoh\exabc"def'}}\keyword{environment}