Skip to contents

Safely constructs a file path from a directory and a filename, independent of whether the directory ends with a trailing slash. The resulting path uses forward slashes.

Usage

buildPath(dir, filename)

Arguments

dir

character string. Directory path.

filename

character string. File name to append to dir.

Value

a character string representing the file path.

Details

Trailing slashes (or backslashes) in dir are removed before the components are joined, so buildPath("data", "file.csv") and buildPath("data/", "file.csv") yield the same result.

The path is then passed through normalizePath()with mustWork = FALSE, so paths that do not (yet) exist are allowed. Note that normalizePath resolves existing paths to absolute form, while non-existing paths are returned as constructed (i.e. possibly relative).

Both arguments are vectorized in the usual file.path() manner.

Note

Converting between forward slashes and backslashes is a frequent necessity – and a hassle – especially in Windows. The cycleSlashes() function in the swissValet package is useful for this purpose.

Examples

buildPath("data", "file.csv")
#> [1] "data/file.csv"
buildPath("data/", "file.csv")
#> [1] "data/file.csv"