R: Express File Paths in Canonical Form: Express File Paths in Canonical Form


Convert file paths to canonical form for the platform, to display them in a user-understandable form and so that relative and absolute paths can be compared.


normalizePath(path, winslash = "\\", mustWork = NA)



character vector of file paths.


the separator to be used on Windows – ignored elsewhere. Must be one of c("/", "\\").


logical: if TRUE then an error is given if the result cannot be determined; if NA then a warning.


Tilde-expansion (see path.expand) is first done on paths.

Where the Unix-alike platform supports it attempts to turn paths into absolute paths in their canonical form (no ./, ../ nor symbolic links). It relies on the POSIX system function realpath: if the platform does not have that (we know of no current example) then the result will be an absolute path but might not be canonical. Even where realpath is used the canonical path need not be unique, for example via hard links or multiple mounts.

On Windows it converts relative paths to absolute paths, converts short names for path elements to long names and ensures the separator is that specified by winslash. It will match paths case-insensitively and return the canonical case. UTF-8-encoded paths not valid in the current locale can be used.

mustWork = FALSE is useful for expressing paths for use in messages.


A character vector.

If an input is not a real path the result is system-dependent (unless mustWork = TRUE, when this should be an error). It will be either the corresponding input element or a transformation of it into an absolute path.

Converting to an absolute file path can fail for a large number of reasons. The most common are


The canonical form of paths may not be what you expect. For example, on macOS absolute paths such as ‘/tmp’ and ‘/var’ are symbolic links.


# random tempdir
cat(normalizePath(c(R.home(), tempdir())), sep = "\n")

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

All documentation is copyright its authors; we didn't write any of that.