Skip to contents

Assert that a filepath includes one of the selected extensions. Does not require file to actually exist.

Usage

assert_file_has_extension(
  x,
  extensions,
  compression = FALSE,
  msg = NULL,
  call = rlang::caller_env(),
  arg_name = NULL
)

Arguments

x

An object

extensions

valid extensions (character vector). Do not include the '.', e.g. supply extensions = 'txt' not extensions = '.txt'

compression

should compression extension ‘.gz’, ‘.bz2’ or ‘.xz’ be removed first?

msg

A character string containing the error message if file x does not have the specified extensions

call

Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details.

arg_name

Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name).

Value

invisible(TRUE) if x has any of the specified extensions, otherwise aborts with the error message specified by msg

Examples

try({
assert_file_has_extension("foo.txt", extensions = "txt") # Passes
assert_file_has_extension("file.txt", extensions = "csv") # Throws Error
})
#> Error in eval(expr, envir) : 
#>   '"file.txt"' has an invalid extension (required extension/s: csv). The
#> following file has an unexpected extension: [file.txt]