R Startup

Anthony Chau

2020/06/23

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:

  1. Value of Sys.getenv("R_USER")
  2. Value of Sys.getenv("HOME")
  3. Use Windows personal directory. Typically: C:\Users\username\Documents
  4. Set to ${HOMEDRIVE}${HOMEPATH} if both Windows environment variables are set.
  5. 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

References/resources on R Startup