R/chk-file.R

Defines functions vld_file chk_file

Documented in chk_file vld_file

#' Check File Exists
#'
#' @description
#' Checks if file exists using
#'
#' `vld_string(x) && file.exists(x) && !dir.exists(x)`
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family chk_files
#'
#' @examples
#' # chk_file
#' try(chk_file(tempfile()))
#' @export
chk_file <- function(x, x_name = NULL) {
  if (vld_file(x)) {
    return(invisible(x))
  }
  if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
  chk_string(x, x_name = x_name)
  if (dir.exists(x)) {
    abort_chk(x_name, " must specify a file ('", x, "' is a directory)", x = x)
  }
  abort_chk(x_name, " must specify an existing file ('", x, "' can't be found)", x = x)
}

#' @describeIn chk_file Validate File Exists
#'
#' @examples
#' # vld_file
#' vld_file(tempfile())
#' @export
vld_file <- function(x) vld_string(x) && file.exists(x) && !dir.exists(x)

Try the chk package in your browser

Any scripts or data that you put into this service are public.

chk documentation built on Oct. 6, 2023, 9:06 a.m.