R/files.R

Defines functions get_filepath normalize_path_te list_file_details_cleanly

Documented in get_filepath list_file_details_cleanly normalize_path_te

#' Create a filepath
#'
#' @description Create a filepath from a directory, filename, and extension.
#' @details None.
#' @param dir character. Directory name.
#' @param filename character. Filename (without extension).
#' @param ext character. File extension (without a period).
#' @return character. Filepath.
#' @export
get_filepath <-
  function(dir,
           filename,
           ext) {
    out <- file.path(dir, paste0(filename, ".", ext))
    # out <- normalize_path_te(out)
    out
  }

#' Normalize a filepath
#'
#' @description Custom version of \code{normalizePath()}.
#' @details Modified from https://github.com/rstudio/rmarkdown/blob/81c209271e06266115e08263dbcde5d007e4d77c/R/includes.R
#' NOTE: Not sure why, but need to set mustWork = FALSE) explicitly, otherwise
#' warnings appear. (This behavior is not observed when calling normalizePath directly.
#' Howver, \code{normalizePath()} specifies \code{winslash = \\} by default, which is annoying.
#' @param filepath character. Filepath.
#' @param winslash character. \code{/} instead of \code{\\}.
#' @param mustWork logical.
#' @return character. Filepath.
#' @export
normalize_path_te <- function(filepath = NULL, winslash = "/", mustWork = NA) {
  if (!is.null(filepath)) {
    out <- normalizePath(path = filepath, winslash = winslash, mustWork = mustWork)
  }
  out
}



#' List file information in a "tidy" fashion
#'
#' @description Custom wrapper for \code{list.files()}.
#' @details Columns are renamed to replicate those output bye \code{fs::file_info} function.
#' @param dir character. Direct paramater to pass to \code{path} argument of \code{list.files()}.
#' @param rgx charager. (Regular expression.) Direct paramater to pass to \code{pattern} argument of \code{list.files()}.
#' @param recursive logical. Direct paramater to pass to \code{list.files()}.
#' @param ... dots. Additional parameters to pass to \code{list.files()}. (Not actually implement.)
#' @param reorder logical. Indicates whether to move path column to last column (for more optimal printing).
#' @param keep_all logical. Indicates whether to keep all of the \code{list.files()} columns.
#' @param cols_exclude character (vector). Columns to exclude.
#' @return data.frame
#' @export
#' @importFrom tibble as_tibble rownames_to_column
#' @importFrom dplyr mutate_at vars contains funs mutate select rename everything one_of
#' @importFrom lubridate ymd_hms
list_file_details_cleanly <-
  function(dir = getwd(),
           rgx = ".",
           recursive = FALSE,
           ...,
           reorder = TRUE,
           keep_all = FALSE,
           cols_exclude = c("mode", "exe")) {
    filepaths_list <-
      list.files(
        path = dir,
        pattern = rgx,
        recursive = recursive,
        full.names = TRUE
      )

    isdir <- rowname <- mtime <- ctime <- atime <- path <- type <- NULL
    out <-
      filepaths_list %>%
      # file.info(recursive = recursive) %>%
      file.info(recursive = FALSE) %>%
      tibble::as_tibble() %>%
      tibble::rownames_to_column() %>%
      dplyr::mutate_at(dplyr::vars(dplyr::contains("time")),
                       dplyr::funs(lubridate::ymd_hms)) %>%
      dplyr::mutate(type = ifelse(isdir, "directory", "file")) %>%
      dplyr::select(-isdir) %>%
      dplyr::rename(path = rowname,
                    modification_time = mtime,
                    birth_time = ctime,
                    access_time = atime)

    if (reorder) {
      out <- dplyr::select(out, type, dplyr::everything())
      out <- dplyr::select(out, -path, dplyr::everything(), path)
    }
    if (!keep_all) {
      # browser()
      out <- dplyr::select(out, -dplyr::one_of(c(cols_exclude)))
    }
    out

  }
aelhabr/teutils documentation built on May 7, 2019, 7:59 a.m.