R/check_dir.R

Defines functions check_dir

Documented in check_dir

#' Directory check
#'
#' Check that
#' 1. The user knows that the data will not be saved if `dir = NULL`.
#' 1. The directory exists if it can be created.
#' 1. The function fails if the directory cannot be created.
#'
#' @template dir
#' @template verbose
#'
#' @author Chantel R. Wetzel
#' @export
#'
#' @examples
#' check_dir(getwd(), verbose = FALSE)
#' # See more output
#' check_dir(getwd())
#'
check_dir <- function(dir, verbose = TRUE){

  if (is.null(dir) || length(dir) == 0) {
    if (verbose) {
      message("Output will not be saved in `dir` because dir = NULL.")
    }
  } else {
    dir.create(dir, showWarnings = FALSE, recursive = TRUE)
    if (!file.exists(dir)) {
      stop(glue::glue(
          "[ENOENT] Failed to make the following directory:
          '{dir}'"
      ))
    }
  }

}
nwfsc-assess/nwfscSurvey documentation built on May 5, 2024, 5:21 a.m.