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.
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.
See also
Other file.path:
fileExistURL(),
findDownload(),
isFilePath(),
isURL(),
splitPath()
