Check if inputs are existing directories or files and throw an error if not.
Usage
check_dir(x, ..., arg = caller_arg(x), call = caller_env())
check_file(
x,
...,
ext = NULL,
case = TRUE,
x_arg = caller_arg(x),
ext_arg = caller_arg(ext),
call = caller_env()
)
check_ext(
x,
ext,
...,
case = TRUE,
x_arg = caller_arg(x),
ext_arg = caller_arg(ext),
call = caller_env()
)Arguments
- x
A path to check.
- ...
Additional arguments passed to
cli_abort()which forwards unmatched arguments toabort().- arg, x_arg, ext_arg
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem.
- call
The execution environment of a currently running function, e.g.
caller_env(). The function will be mentioned in error messages as the source of the error. See thecallargument ofcli_abort()for more information.- ext
A character vector of file extensions to check for.
- case
A logical value indicating if the extension check should be case-sensitive. If
FALSE, the check will be case-insensitive.
Note
The checking of extensions is done simply using endsWith().
Examples
x <- file.path(R.home(), "library", "stats")
check_dir(x)
check_file(x) |> try()
#> Error in eval(expr, envir) :
#> `x` must be an existing file, but it is a directory.
#> ℹ Path provided: /opt/R/4.6.1/lib/R/library/stats.
x <- file.path(x, "DESCRIPTION")
check_file(x)
check_dir(x) |> try()
#> Error in eval(expr, envir) :
#> `x` must be an existing directory, but it is a file.
#> ℹ Path provided: /opt/R/4.6.1/lib/R/library/stats/DESCRIPTION.
check_file(x, ext = c(".csv", ".xlsx")) |> try()
#> Error in eval(expr, envir) :
#> `x` must have extension ".csv" or ".xlsx".
