R/sniff.R

Defines functions sniff_dfs

Documented in sniff_dfs

#' @title Read multiple delimited files
#' @description Wrapper around [`data.table::fread()`] to read multiple files.
#' @param files A list of files, connections, or literal data.
#' @param rename A named vector of regex and replacements used to rename columns in each file.
#' @param ... One or more arguments separated with a comma to pass on to [`data.table::fread()`].
#' @param merge Should the data be merged in a single data frame?
#' @param clean_names Should the column names be cleaned?
#' @param ignore_case Should letter case be ignored?
#' @param .id Either a string or NULL. See [`purrr::map_df()`] for details.
#' @export
sniff_dfs <- function(files, rename = NULL, ..., merge = TRUE, clean_names = TRUE, ignore_case = TRUE, .id = NULL) {
  fun <- function(x, .rename = rename) {
    x <- as_tibble(data.table::fread(x, ...))
    if (!is.null(.rename)) {
      x <- rename_with(x, string_replace_all, pattern = .rename, ignore_case = ignore_case)
    }
    if (isTRUE(clean_names)) {
      x <- clean_names(x)
    }
    x
  }

  if (isTRUE(merge)) {
    map_df(.x = files, .f = fun, .id = .id)
  } else {
    map(.x = files, .f = fun)
  }
}
arnaudgallou/toolkit documentation built on Nov. 25, 2022, 5:42 p.m.