R/is.R

Defines functions is_absolute_path is_file_empty is_link is_dir is_file

Documented in is_absolute_path is_dir is_file is_file_empty is_link

#' Functions to test for file types
#'
#' @return A named logical vector, where the names give the paths. If the given
#'   object does not exist, `NA` is returned.
#' @seealso [file_exists()], [dir_exists()] and [link_exists()] if you want
#'   to ensure that the path also exists.
#' @template fs
#' @importFrom stats setNames
#' @inheritParams file_info
#' @export
#' @examples
#' \dontshow{.old_wd <- setwd(tempdir())}
#' dir_create("d")
#'
#' file_create("d/file.txt")
#' dir_create("d/dir")
#' link_create(path(path_abs("d"), "file.txt"), "d/link")
#'
#' paths <- dir_ls("d")
#' is_file(paths)
#' is_dir(paths)
#' is_link(paths)
#'
#' # Cleanup
#' dir_delete("d")
#' \dontshow{setwd(.old_wd)}
is_file <- function(path, follow = TRUE) {
  res <- file_info(path, follow = follow)
  setNames(!is.na(res$type) & res$type == "file", path)
}

#' @rdname is_file
#' @export
is_dir <- function(path, follow = TRUE) {
  res <- file_info(path, follow = follow)
  setNames(!is.na(res$type) & res$type == "directory", path)
}

#' @rdname is_file
#' @export
is_link <- function(path) {
  res <- file_info(path)
  setNames(!is.na(res$type) & res$type == "symlink", path)
}

#' @rdname is_file
#' @export
is_file_empty <- function(path, follow = TRUE) {
  res <- file_info(path, follow = follow)

  setNames(!is.na(res$size) & res$size == 0, res$path)
}

#' Test if a path is an absolute path
#'
#' @template fs
#' @export
#' @examples
#' is_absolute_path("/foo")
#' is_absolute_path("C:\\foo")
#' is_absolute_path("\\\\myserver\\foo\\bar")
#'
#' is_absolute_path("foo/bar")
is_absolute_path <- function(path) {
  tidy_path <- path_tidy(path)
  grepl("^~", path) | grepl("^(/+|[A-Za-z]:)", tidy_path)
}

Try the fs package in your browser

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

fs documentation built on May 29, 2024, 12:28 p.m.