Skip to contents

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 to abort().

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 the call argument of cli_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.

Value

NULL invisibly if the check passes, otherwise an error is thrown.

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".