From f3455ae392f6d01e6bb36d9bb05e995c2086a1ea Mon Sep 17 00:00:00 2001 From: hornik Date: Thu, 3 Apr 2008 09:48:26 +0000 Subject: [PATCH] Allow specifying user environment and profile files via environment 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 --- doc/Rscript.1 | 2 +- doc/manual/R-intro.texi | 29 +++++++++++++++++------------ src/library/base/man/Startup.Rd | 28 +++++++++++++++++----------- src/main/Renviron.c | 7 ++++++- src/scripts/R.sh.in | 2 +- src/unix/Rscript.c | 2 +- src/unix/sys-unix.c | 12 ++++++++---- 7 files changed, 51 insertions(+), 31 deletions(-) diff --git a/doc/Rscript.1 b/doc/Rscript.1 index bdd6d67396e..6fc73c6c4e3 100644 --- a/doc/Rscript.1 +++ b/doc/Rscript.1 @@ -45,7 +45,7 @@ Don't read the site and user environment files Don't read the site-wide Rprofile .TP \fB\-\-no\-init\-file\fR -Don't read the .Rprofile or ~/.Rprofile files +Don't read the user R profile .TP \fB\-\-restore\fR Do restore previously saved objects at startup diff --git a/doc/manual/R-intro.texi b/doc/manual/R-intro.texi index 66f7d6a9966..7fd687c12ce 100644 --- a/doc/manual/R-intro.texi +++ b/doc/manual/R-intro.texi @@ -4033,7 +4033,8 @@ users control over their workspace and allows for different startup procedures in different working directories. If no @file{.Rprofile} 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 -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 in the @file{.RData} image has a special status. It is automatically @@ -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. 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. @example @@ -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 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 -used (if it exists). The user file searched for is @file{.Renviron} -in the current or in the user's home directory (in that order). These -files should contain lines of the form @samp{@var{name}=@var{value}}. -(See @code{help(Startup)} for a precise description.) Variables you -might want to set include @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). +used (if it exists). The user file is the one pointed to by the +environment variable @env{R_ENVIRON_USER} if this is set; otherwise, +files @file{.Renviron} in the current or in the user's home directory +(in that order) are searched for. These files should contain lines of +the form @samp{@var{name}=@var{value}}. (See @code{help("Startup")} for +a precise description.) Variables you might want to set include +@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 Then @R{} searches for the site-wide startup profile unless the command @@ -6813,9 +6816,11 @@ that variable is unset, the default @file{$R_HOME/etc/Rprofile.site} is used if this exists. @item -Then, unless @option{--no-init-file} was given, @R{} searches for a file -called @file{.Rprofile} in the current directory or in the user's home -directory (in that order) and sources it. +Then, unless @option{--no-init-file} was given, @R{} searches for a user +profile and sources it. The name of this file is taken from the +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 It also loads a saved image from @file{.RData} if there is one (unless diff --git a/src/library/base/man/Startup.Rd b/src/library/base/man/Startup.Rd index f1829bfcf11..a34f8eea01a 100644 --- a/src/library/base/man/Startup.Rd +++ b/src/library/base/man/Startup.Rd @@ -31,8 +31,10 @@ 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 does not in a \sQuote{factory-fresh} installation). - The user files searched for are \file{.Renviron} in the current or in - the user's home directory (in that order). + The name of the user 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 (in + that order). #ifdef windows (See \sQuote{See also} for how the home directory is found on Windows.) #endif @@ -55,10 +57,12 @@ is normally advisable to use \code{\link{local}} if code needs to be 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 - home directory (in that order) and sources it into the user - workspace. + home directory (in that order) is searched for. The user profile is + sourced 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 be @@ -126,15 +130,17 @@ } \note{ #ifdef unix - The file \file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron} is always read very early in - the start-up processing. It contains environment variables set by \R - in the configure process. Values in that file can be overridden in - site or user environment files: do not change - \file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron} itself. Note that this is distinct from + The file \file{\var{\link[base:Rhome]{R\_HOME}}/etc/Renviron} is + always read very early in the start-up processing. It contains + environment variables set by \R in the configure process. Values in + that file can be overridden in site or user environment files: do not + 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}. #endif #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 environment variables set by \R in the configure process, and is not used on \R for Windows. diff --git a/src/main/Renviron.c b/src/main/Renviron.c index acd62d23471..f550df3a5a9 100644 --- a/src/main/Renviron.c +++ b/src/main/Renviron.c @@ -255,7 +255,12 @@ void process_site_Renviron () /* try user Renviron: ./.Renviron, then ~/.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; #ifdef Unix diff --git a/src/scripts/R.sh.in b/src/scripts/R.sh.in index 1abfea65b38..5d1b37afec0 100755 --- a/src/scripts/R.sh.in +++ b/src/scripts/R.sh.in @@ -61,7 +61,7 @@ Options: --no-save Don't save it --no-environ Don't read the site and user environment files --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 --no-restore-data Don't restore previously saved objects --no-restore-history Don't restore the R history file diff --git a/src/unix/Rscript.c b/src/unix/Rscript.c index 52238053e58..0a463b37fa3 100644 --- a/src/unix/Rscript.c +++ b/src/unix/Rscript.c @@ -89,7 +89,7 @@ void usage(void) 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-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, " --vanilla Combine --no-save, --no-restore, --no-site-file\n"); fprintf(stderr, " --no-init-file and --no-environ\n"); diff --git a/src/unix/sys-unix.c b/src/unix/sys-unix.c index ef85d23c18d..1b8768da99a 100644 --- a/src/unix/sys-unix.c +++ b/src/unix/sys-unix.c @@ -59,17 +59,21 @@ extern Rboolean LoadInitFile; attribute_hidden FILE *R_OpenInitFile(void) { - char buf[256], *home; + char buf[256], *home, *p = getenv("R_PROFILE_USER"); FILE *fp; fp = NULL; if (LoadInitFile) { - if ((fp = R_fopen(".Rprofile", "r"))) + if(p && strlen(p)) { + fp = R_fopen(R_ExpandFileName(p), "r"); return fp; - if ((home = getenv("HOME")) == NULL) + } + if((fp = R_fopen(".Rprofile", "r"))) + return fp; + if((home = getenv("HOME")) == NULL) return NULL; sprintf(buf, "%s/.Rprofile", home); - if ((fp = R_fopen(buf, "r"))) + if((fp = R_fopen(buf, "r"))) return fp; } return fp;