Windows and the HOME directory

r
configuration
home
windows
Author

Nicolas Casajus

Published

March 11, 2024

Windows-only

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.).

Home vs. working directory

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 terminal
  • C:/Users/username/Documents on RStudio IDE

And the behavior of various functions is inconsistent. For instance:

path.expand("~")
## C:/Users/username/Documents

normalizePath("~")
## C:\\Users\\username\\Documents

fs::path_home()
## C:/Users/username

Changing HOME directory

Danger

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' (replace username by your Windows user name).
  • Save the file.
  • Copy this .Renviron.site file in C:/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 in C:/Program Files/R/R-X.X.X/etc/ and add this line: R_USER='C:/Users/username' (replace username by your Windows user name).
  • Save the file.

After restarting , run these lines:

Sys.getenv("R_USER")
## C:/Users/username

path.expand("~")
## C:/Users/username

normalizePath("~")
## C:\\Users\\username

fs::path_home()
## C:/Users/username

From now you should have the same outputs on RStudio IDE and on a Terminal.

This is a temporary fix

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 (replace username by your Windows user name)

After restarting , the HOME directory should always point to C:/Users/username.

Sys.getenv("R_USER")
## C:/Users/username

path.expand("~")
## C:/Users/username

normalizePath("~")
## C:\\Users\\username

fs::path_home()
## C:/Users/username