This post only concerns Windows users.
Context
In common operating systems, there is an important directory: the HOME
directory (also known as the User’s home directory). This directory contains the user’s main folders (Desktop/
, Documents/
, Pictures/
, etc.) but it can also be used by software to store user’s configuration files (ssh
, bash
, zsh
, etc.).
The working directory is the directory from which was launched. The function getwd()
can be run to find this directory. This directory changes between projects. The home directory is user specific and the symbol ~
is often used to refer to this HOME
directory.
also uses this HOME
directory to store two configuration files: the .Renviron
and .Rprofile
files (run ?Startup
to learn more about these files).
On Unix systems ( and ), the HOME
directory is /home/username
(or /Users/username
on Apple computers).
On Windows , depending on whether you use RStudio IDE (and therefore Rgui.exe
) or in a terminal (and therefore Rterm.exe
), the value of this HOME
directory can be different:
C:/Users/username
on a terminalC:/Users/username/Documents
on RStudio IDE
And the behavior of various functions is inconsistent. For instance:
Changing HOME directory
Do not follow this tip if you are several users on the same computer.
Here we will resolve the discrepancy between these two different directories by using C:/Users/username
everywhere (like in Unix systems).
As mentioned in the R for Windows FAQ (section 2.13), we can modify this HOME
directory at the system level by setting the environment variable R_USER
.
This environment variable can be modified by creating a .Renviron.site
file in the directory C:/Program Files/R/R-X.X.X/etc/
(where R-X.X.X
is the version of ). This file is used to store environment variables at the system level (so for all users).
N.B. You need to have permission to create/edit this file.
Proceed as follow if the .Renviron.site
file does not exist in C:/Program Files/R/R-X.X.X/etc/
:
- In
C:/Users/username/Desktop/
, create an empty file.Renviron.site
. - Open this file and add this line:
R_USER='C:/Users/username'
(replaceusername
by your Windows user name). - Save the file.
- Copy this
.Renviron.site
file inC:/Program Files/R/R-X.X.X/etc/
.
Proceed as follow if the .Renviron.site
file already exists in C:/Program Files/R/R-X.X.X/etc/
:
- Open the
.Renviron.site
file inC:/Program Files/R/R-X.X.X/etc/
and add this line:R_USER='C:/Users/username'
(replaceusername
by your Windows user name). - Save the file.
After restarting , run these lines:
From now you should have the same outputs on RStudio IDE and on a Terminal.
You will need to repeat this tip each time you reinstall/upgrade .
Edit (2024/03/12)
With some functions, this tip is not enough. For instance, the functions utils::file.edit("~/.Renviron")
, utils::file.edit("~/.Rprofile")
, usethis::edit_r_environ()
, usethis::edit_r_profile()
do not recognized the new HOME
directory.
To fix this issue, we need to define the R_USER
environment variable at the Windows level.
Proceed as follow:
- In the Windows search bar, type
variables
and open Modify system environment variables. - At the bottom, click on Environment variables…
- In the section System variable, add a new entry and set:
- Name:
R_USER
- Value:
C:\Users\username
(replaceusername
by your Windows user name)
- Name:
After restarting , the HOME
directory should always point to C:/Users/username
.