Upon startup, R and RStudio look for a few different files you can use to control the behavior of your R session, for example by setting options or environment variables. In the context of RStudio Team, these settings are often used to set RStudio Server Pro to search for packages in an RStudio Package Manager repository.
This article is a practical guide to how to set particular options on R startup. General information on how to manage R package environments is available at environments.rstudio.com , and a deeper treatment of R process startup is available in this article.
Here is a summary table of how to control R options and environment variables on startup. More details are below.
||User or Admin||User or Project||None, sourced as R code.|
||User or Admin||User or Project||Set environment variables only.|
||Admin||Version of R||None, sourced as R code.|
||Admin||Version of R||Set environment variables only.|
||Admin||Server||Only RStudio settings, only single repository.|
||Admin||Server||Only for setting repositories.|
.Rprofile files are user-controllable files to set options and environment variables.
.Rprofile files can be either at the user or project level. User-level
.Rprofile files live in the base of the user's home directory, and project-level
.Rprofile files live in the base of the project directory.
R will source only one
.Rprofile file. So if you have both a project-specific
.Rprofile file and a user
.Rprofile file that you want to use, you explicitly source the user-level
.Rprofile at the top of your project-level
.Rprofile files are sourced as regular R code, so setting environment variables must be done inside a
Sys.setenv(key = "value") call.
One easy way to edit your
.Rprofile file is to use the
usethis::edit_r_profile() function from within an R session. You can specify whether you want to edit the user or project level
.Renviron is a user-controllable file that can be used to create environment variables. This is especially useful to avoid including credentials like API keys inside R scripts. This file is written in a key-value format, so environment variables are created in the format:
Sys.getenv("Key1") will return
"value1" in an R session.
Like with the
.Renviron files can be at either the user or project level. If there is a project-level
.Renviron, the user-level file will not be sourced. The
usethis package includes a helper function for editing
.Renviron files from an R session with
.Renviron files have equivalents that apply server wide.
Renviron.site (no leading dot) files are managed by admins on RStudio Server and are specific to a particular version of R. The most common settings for these files involve access to package repositories. For example, using the shared-baseline package management strategy is generally done from an
Users can override settings in these files with their individual
These files are set for each version of R and should be located in
R_HOME/etc/. You can find
R_HOME by running the command
= "home") in a session of that version of R. So, for example, if you find that
Rprofile.site for R 3.6.2 would go in
RStudio Server allows server admins to configure particular server-wide R package repositories via the
repos.conf files. Only one repository can be configured in
rsession.conf. If multiple repositories are needed,
repos.conf should be used. Details on configuring RStudio Server with these files are in this support article.