Sometimes configuration can be a pain – especially on Windows where many things can happen in the background. According to the R documentation the .Rprofile file can be placed in three different places and will be read during startup.
- Rprofile.site – this is usually within $R_HOME/etc/Rprofile.site and does not exist on factory installations aka fresh installations
- $R_HOME/.Rprofile – this file is in the user homedirectory
- .Rprofile – this file is in your current working directory
The .Rprofile file can take general options that you want to inject into your R session every single time to provide default values. This could be default options, changing the prompt, output printing options and many more.
Find the location
Finding the location for the second .Rprofile file can be challenging. It’s pretty easy for Linux as .Rprofile usually lives happily in your $HOME directory.
For Windows things may be a bit more complicated. The R documentation FAQ mentions a method for finding the correct location. Start R and type the following things in your R console and R will print out the path to your R_HOME and that’s where the .Rprofile file belongs to.
> Sys.getenv("R_USER")  "//fshome/home$/rhaen/My Documents"
As you can see in this example from my Windows laptop the local IT departments prefers to have the home directory on a file server. Therefore the full path to the Rprofile will be:
Some thoughts about the location
You have to keep in mind that there is always a certain load order for the Rprofile files. The general load order is the following:
- .Rprofile in the current directory
- R_HOME/.Rprofile in your R_HOME directory
The load order matters and we can make use of that order for specific tasks. I tend to store password/oauth tokens within my .Rprofile file inside my projects. If I work on a project I’ll switch to project directory and start my R from that directory. That causes R to load the project oriented .Rprofile with the necessary tokens in it.
All other things which are not project specific are inside my R_HOME/.Rprofile file. Usually I store things like common used options like: R_LIBS, PAGER and my personal R prompt in there. Common used packages can be loaded from there, too.
Can I use the SHELL/OS environment for settings?
Being a maintainer for software and all different kinds of environments I think it’s not the best way to use the .bash_profile or the .bashrc for settings for the R interpreter. The reason for this pretty simple. Those shell specific files are not always processed and you need to have a good unix background to understand when those files are being read and when not. Especially for “production grade” deployments with R I would highly recommend to have a dedicated configuration file and load it – or use a .Rprofile file inside your R application/deployment.
While we are at it: use a version control system for this. It doesn’t matter which one as long as you stick to it!