Allows a user to specify that their character string is indeed a filepath. Thus, methods that require only a filepath can be dispatched correctly.
asPath(obj, nParentDirs = 0)
# S3 method for class 'character'
asPath(obj, nParentDirs = 0)
# S3 method for class 'null'
asPath(obj, nParentDirs = 0)
A vector of class Path
, which is similar to a character, but
has an attribute indicating how deep the Path should be
considered "digestible". In other words, most of the time, only some
component of an absolute path is relevant for evaluating its purpose in
a Cache situation. In general, this is usually equivalent to just the "relative" path
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