Skip to content

Commit

Permalink
Allow specifying user environment and profile files via environment
Browse files Browse the repository at this point in the history
variables 'R_ENVIRON_USER' and 'R_PROFILE_USER', respectively.

git-svn-id: https://svn.r-project.org/R/trunk@45053 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
hornik committed Apr 3, 2008
1 parent 6ea6ec5 commit f3455ae
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 31 deletions.
2 changes: 1 addition & 1 deletion doc/Rscript.1
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Don't read the site and user environment files
Don't read the site-wide Rprofile Don't read the site-wide Rprofile
.TP .TP
\fB\-\-no\-init\-file\fR \fB\-\-no\-init\-file\fR
Don't read the .Rprofile or ~/.Rprofile files Don't read the user R profile
.TP .TP
\fB\-\-restore\fR \fB\-\-restore\fR
Do restore previously saved objects at startup Do restore previously saved objects at startup
Expand Down
29 changes: 17 additions & 12 deletions doc/manual/R-intro.texi
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4033,7 +4033,8 @@ users control over their workspace and allows for different startup
procedures in different working directories. If no @file{.Rprofile} procedures in different working directories. If no @file{.Rprofile}
file is found in the startup directory, then @R{} looks for a file is found in the startup directory, then @R{} looks for a
@file{.Rprofile} file in the user's home directory and uses that (if it @file{.Rprofile} file in the user's home directory and uses that (if it
exists). exists). If the environment variable @env{R_PROFILE_USER} is set, the
file it points to is used instead of the @file{.Rprofile} files.


Any function named @code{.First()} in either of the two profile files or Any function named @code{.First()} in either of the two profile files or
in the @file{.RData} image has a special status. It is automatically in the @file{.RData} image has a special status. It is automatically
Expand All @@ -4043,7 +4044,7 @@ below alters the prompt to @code{$} and sets up various other useful
things that can then be taken for granted in the rest of the session. things that can then be taken for granted in the rest of the session.


Thus, the sequence in which files are executed is, @file{Rprofile.site}, Thus, the sequence in which files are executed is, @file{Rprofile.site},
@file{.Rprofile}, @file{.RData} and then @code{.First()}. A definition the user profile, @file{.RData} and then @code{.First()}. A definition
in later files will mask definitions in earlier files. in later files will mask definitions in earlier files.


@example @example
Expand Down Expand Up @@ -6797,13 +6798,15 @@ Unless @option{--no-environ} was given, @R{} searches for user and site
files to process for setting environment variables. The name of the files to process for setting environment variables. The name of the
site file is the one pointed to by the environment variable site file is the one pointed to by the environment variable
@env{R_ENVIRON}; if this is unset, @file{$R_HOME/etc/Renviron.site} is @env{R_ENVIRON}; if this is unset, @file{$R_HOME/etc/Renviron.site} is
used (if it exists). The user file searched for is @file{.Renviron} used (if it exists). The user file is the one pointed to by the
in the current or in the user's home directory (in that order). These environment variable @env{R_ENVIRON_USER} if this is set; otherwise,
files should contain lines of the form @samp{@var{name}=@var{value}}. files @file{.Renviron} in the current or in the user's home directory
(See @code{help(Startup)} for a precise description.) Variables you (in that order) are searched for. These files should contain lines of
might want to set include @env{R_PAPERSIZE} (the default paper size), the form @samp{@var{name}=@var{value}}. (See @code{help("Startup")} for
@env{R_PRINTCMD} (the default print command) and @env{R_LIBS} (specifies a precise description.) Variables you might want to set include
the list of @R{} library trees searched for add-on packages). @env{R_PAPERSIZE} (the default paper size), @env{R_PRINTCMD} (the
default print command) and @env{R_LIBS} (specifies the list of @R{}
library trees searched for add-on packages).


@item @item
Then @R{} searches for the site-wide startup profile unless the command Then @R{} searches for the site-wide startup profile unless the command
Expand All @@ -6813,9 +6816,11 @@ that variable is unset, the default @file{$R_HOME/etc/Rprofile.site} is
used if this exists. used if this exists.


@item @item
Then, unless @option{--no-init-file} was given, @R{} searches for a file Then, unless @option{--no-init-file} was given, @R{} searches for a user
called @file{.Rprofile} in the current directory or in the user's home profile and sources it. The name of this file is taken from the
directory (in that order) and sources it. environment variable @env{R_PROFILE_USER}; if unset, a file called
@file{.Rprofile} in the current directory or in the user's home
directory (in that order) is searched for.


@item @item
It also loads a saved image from @file{.RData} if there is one (unless It also loads a saved image from @file{.RData} if there is one (unless
Expand Down
28 changes: 17 additions & 11 deletions src/library/base/man/Startup.Rd
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
environment variable \env{R\_ENVIRON}; if this is unset or empty, environment 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 it \file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron.site} is used (if it exists, which it
does not in a \sQuote{factory-fresh} installation). does not in a \sQuote{factory-fresh} installation).
The user files searched for are \file{.Renviron} in the current or in The name of the user can be specified by the \env{R\_ENVIRON\_USER}
the user's home directory (in that order). environment variable; if this is unset, the user files searched for are
\file{.Renviron} in the current or in the user's home directory (in
that order).
#ifdef windows #ifdef windows
(See \sQuote{See also} for how the home directory is found on Windows.) (See \sQuote{See also} for how the home directory is found on Windows.)
#endif #endif
Expand All @@ -55,10 +57,12 @@
is normally advisable to use \code{\link{local}} if code needs to be is normally advisable to use \code{\link{local}} if code needs to be
executed: see the examples. executed: see the examples.
Then, unless \option{--no-init-file} was given, \R searches for a file Then, unless \option{--no-init-file} was given, \R searches for a user
profile. The name of this file can be specified by the
\env{R\_PROFILE\_USER} environment variable. If this is unset, a file
called \file{.Rprofile} in the current directory or in the user's called \file{.Rprofile} in the current directory or in the user's
home directory (in that order) and sources it into the user home directory (in that order) is searched for. The user profile is
workspace. sourced into the user workspace.


Note that when the site and user profile files are sourced only the 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 be \pkg{base} package is loaded, so objects in other packages need to be
Expand Down Expand Up @@ -126,15 +130,17 @@
} }
\note{ \note{
#ifdef unix #ifdef unix
The file \file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron} is always read very early in The file \file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron} is
the start-up processing. It contains environment variables set by \R always read very early in the start-up processing. It contains
in the configure process. Values in that file can be overridden in environment variables set by \R in the configure process. Values in
site or user environment files: do not change that file can be overridden in site or user environment files: do not
\file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron} itself. Note that this is distinct from change \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}. \file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron.site}.
#endif #endif
#ifdef windows #ifdef windows
Unix versions of \R have a file \file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron} Unix 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 contains which is read very early in the start-up processing. It contains
environment variables set by \R in the configure process, and is not environment variables set by \R in the configure process, and is not
used on \R for Windows. used on \R for Windows.
Expand Down
7 changes: 6 additions & 1 deletion src/main/Renviron.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ void process_site_Renviron ()
/* try user Renviron: ./.Renviron, then ~/.Renviron */ /* try user Renviron: ./.Renviron, then ~/.Renviron */
void process_user_Renviron() void process_user_Renviron()
{ {
const char *s; const char *s = getenv("R_ENVIRON_USER");

if(s && strlen(s)) {
process_Renviron(R_ExpandFileName(s));
return;
}


if(process_Renviron(".Renviron")) return; if(process_Renviron(".Renviron")) return;
#ifdef Unix #ifdef Unix
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/R.sh.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Options:
--no-save Don't save it --no-save Don't save it
--no-environ Don't read the site and user environment files --no-environ Don't read the site and user environment files
--no-site-file Don't read the site-wide Rprofile --no-site-file Don't read the site-wide Rprofile
--no-init-file Don't read the .Rprofile or ~/.Rprofile files --no-init-file Don't read the user R profile
--restore Do restore previously saved objects at startup --restore Do restore previously saved objects at startup
--no-restore-data Don't restore previously saved objects --no-restore-data Don't restore previously saved objects
--no-restore-history Don't restore the R history file --no-restore-history Don't restore the R history file
Expand Down
2 changes: 1 addition & 1 deletion src/unix/Rscript.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void usage(void)
fprintf(stderr, " --save Do save workspace at the end of the session\n"); fprintf(stderr, " --save Do save workspace at the end of the session\n");
fprintf(stderr, " --no-environ Don't read the site and user environment files\n"); fprintf(stderr, " --no-environ Don't read the site and user environment files\n");
fprintf(stderr, " --no-site-file Don't read the site-wide Rprofile\n"); fprintf(stderr, " --no-site-file Don't read the site-wide Rprofile\n");
fprintf(stderr, " --no-init-file Don't read the .Rprofile or ~/.Rprofile files\n"); fprintf(stderr, " --no-init-file Don't read the user R profile\n");
fprintf(stderr, " --restore Do restore previously saved objects at startup\n"); fprintf(stderr, " --restore Do restore previously saved objects at startup\n");
fprintf(stderr, " --vanilla Combine --no-save, --no-restore, --no-site-file\n"); fprintf(stderr, " --vanilla Combine --no-save, --no-restore, --no-site-file\n");
fprintf(stderr, " --no-init-file and --no-environ\n"); fprintf(stderr, " --no-init-file and --no-environ\n");
Expand Down
12 changes: 8 additions & 4 deletions src/unix/sys-unix.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ extern Rboolean LoadInitFile;
attribute_hidden attribute_hidden
FILE *R_OpenInitFile(void) FILE *R_OpenInitFile(void)
{ {
char buf[256], *home; char buf[256], *home, *p = getenv("R_PROFILE_USER");
FILE *fp; FILE *fp;


fp = NULL; fp = NULL;
if (LoadInitFile) { if (LoadInitFile) {
if ((fp = R_fopen(".Rprofile", "r"))) if(p && strlen(p)) {
fp = R_fopen(R_ExpandFileName(p), "r");
return fp; return fp;
if ((home = getenv("HOME")) == NULL) }
if((fp = R_fopen(".Rprofile", "r")))
return fp;
if((home = getenv("HOME")) == NULL)
return NULL; return NULL;
sprintf(buf, "%s/.Rprofile", home); sprintf(buf, "%s/.Rprofile", home);
if ((fp = R_fopen(buf, "r"))) if((fp = R_fopen(buf, "r")))
return fp; return fp;
} }
return fp; return fp;
Expand Down

0 comments on commit f3455ae

Please sign in to comment.