path_expand: Finding the User Home Directory

View source: R/path.R

path_expandR Documentation

Finding the User Home Directory

Description

  • path_expand() performs tilde expansion on a path, replacing instances of ~ or ~user with the user's home directory.

  • path_home() constructs a path within the expanded users home directory, calling it with no arguments can be useful to verify what fs considers the home directory.

  • path_expand_r() and path_home_r() are equivalents which always use R's definition of the home directory.

Usage

path_expand(path)

path_expand_r(path)

path_home(...)

path_home_r(...)

Arguments

path

A character vector of one or more paths.

...

Additional paths appended to the home directory by path().

Details

path_expand() differs from base::path.expand() in the interpretation of the home directory of Windows. In particular path_expand() uses the path set in the USERPROFILE environment variable and, if unset, then uses HOMEDRIVE/HOMEPATH.

In contrast base::path.expand() first checks for R_USER then HOME, which in the default configuration of R on Windows are both set to the user's document directory, e.g. ⁠C:\\Users\\username\\Documents⁠. base::path.expand() also does not support ~otheruser syntax on Windows, whereas path_expand() does support this syntax on all systems.

This definition makes fs more consistent with the definition of home directory used on Windows in other languages, such as python and rust. This is also more compatible with external tools such as git and ssh, both of which put user-level files in USERPROFILE by default. It also allows you to write portable paths, such as ⁠~/Desktop⁠ that points to the Desktop location on Windows, macOS and (most) Linux systems.

Users can set the R_FS_HOME environment variable to override the definitions on any platform.

See Also

R for Windows FAQ - 2.14 for behavior of base::path.expand().

Examples

# Expand a path
path_expand("~/bin")

# You can use `path_home()` without arguments to see what is being used as
# the home diretory.
path_home()
path_home("R")

# This will likely differ from the above on Windows
path_home_r()

fs documentation built on Oct. 30, 2024, 9:06 a.m.