Allows a user to specify that their character string is indeed a filepath. Thus, methods that require only a filepath can be dispatched correctly.

If using as("string", "Path"), there is no option to pass nParentDirs. So, using asPath directly (e.g., asPath("string", 0))) is preferred.

asPath(obj, nParentDirs = 0)

# S3 method for character
asPath(obj, nParentDirs = 0)



A character string to convert to a Path.


A numeric indicating the number of parent directories starting from basename(obj) = 0 to keep for the digest


It is often difficult or impossible to know algorithmically whether a character string corresponds to a valid filepath. In the case where it is en existing file, file.exists can work. But if it does not yet exist, e.g., for a save, it is difficult to know whether it is a valid path before attempting to save to the path.

This function can be used to remove any ambiguity about whether a character string is a path. It is primarily useful for achieving repeatability with Caching. Essentially, when Caching, arguments that are character strings should generally be digested verbatim, i.e., it must be an exact copy for the Cache mechanism to detect a candidate for recovery from the cache. Paths, are different. While they are character strings, there are many ways to write the same path. Examples of identical meaning, but different character strings are: path expanding of ~ vs. not, double back slash vs. single forward slash, relative path vs. absolute path. All of these should be assessed for their actual file or directory location, NOT their character string. By converting all character string that are actual file or directory paths with this function, then Cache will correctly assess the location, NOT the character string representation.


tmpf <- tempfile(fileext = ".csv") file.exists(tmpf) ## FALSE
#> [1] FALSE
tmpfPath <- asPath(tmpf) is(tmpf, "Path") ## FALSE
#> [1] FALSE
is(tmpfPath, "Path") ## TRUE
#> [1] TRUE