Some Background:
This post outlines what happens behind the scenes before a new R session is loaded. Note that this post does not go intofine detail about everything that happens. More details can found by typing ?Startup.
We focus on variations of Rprofile and Renviron files that are crucial to the R Startup process. Respectively, these file store R code and environment variables.
The effect of these files will have a different scope that depends on where they are stored. There are three scopes: system, user, and project level. We discuss each scope below.
Scopes
System Level
At the system level, we create Rprofile.site and Renviron.site files. They must have this exact naming. These files are specific to a version of R and affect all users.
We store these system level files in a subdirectory of the R home directory. You can find the path to the R home directory by typing Sys.getenv("R_HOME") in the R console. On windows, the path will typically look like: C:\Program Files\R\R-4.0.1.
Now, we actually store Rprofile.site and Renviron.site in the etc subdirectory of $R_HOME.
User level
At the user level, we create .Rprofile and .Renviron files. These files affect only 1 user.
We store these files in the base of a user’s home directory. I know I have been confused by what is meant by a “home directory”. It is an ambiguous term, especially on Windows. But, this document clarifies on this notion.
Below is the ordered process R uses to set your home directory on Windows:
- Value of
Sys.getenv("R_USER") - Value of
Sys.getenv("HOME") - Use Windows personal directory. Typically:
C:\Users\username\Documents - Set to
${HOMEDRIVE}${HOMEPATH}if both Windows environment variables are set. - Current working directory
Project Level
At the project level, we create .Rprofile and .Renviron files. These files only affect 1 project
We store these files in the base of a project directory. For context, a project directory results from creating a R project within RStudio. That is, project directories contain a .Rroj file.
Extra notes
- If there are system level files, they will always be loaded.
- If there are both user and project level files, only the project level file will be loaded.
- So at most, only a system level file and either a user or project level file will be loaded.
- Do NOT edit the
Renvironfile at$R_HOME/etc - Do not put R code in any
Renvironfiles. These text files will only contain text of the formNAME_OF_ENV_VAR=VALUE