e_read_data_subdir_into_lists: Read data from subdirectories into a structured list

View source: R/e_read_data_subdir_into_lists.R

e_read_data_subdir_into_listsR Documentation

Read data from subdirectories into a structured list

Description

Recurse through subdirectories returning either filenames or tibbles (data.frames) by reading csv, xls, or xlsx with e_read_data_files().

Usage

e_read_data_subdir_into_lists(
  fn_path = ".",
  fn_detect = c("csv$", "xls$", "xlsx$"),
  sw_fn_or_dat = c("fn", "dat")[1],
  sw_exclude_empty_dir = c(TRUE, FALSE)[1],
  sw_dat_add_col_path_fn = c(TRUE, FALSE)[1],
  sw_dat_print_fn_read = c(TRUE, FALSE)[2],
  excel_sheets = "all",
  sw_clean_names = c(TRUE, FALSE)[2],
  sw_list_or_flat = c("list", "flat")[1],
  excel_range = NULL,
  excel_col_names = TRUE,
  sw_delim = c(FALSE, "|")[1],
  sw_read_package_csv_txt = c("readr", "utils")[1]
)

Arguments

fn_path

Starting directory path

fn_detect

NULL for all. File specification, used by stringr::str_detect(), usually specifying file extensions.

sw_fn_or_dat

Return filenames or tibbles (data.frames)

sw_exclude_empty_dir

T/F exclude empty directories

sw_dat_add_col_path_fn

T/F for data, add two columns specifying the directory (DIR__) and filename (FILE__)

sw_dat_print_fn_read

T/F print file names and dimensions as the files are read

excel_sheets

"all" for all sheets, or a list of numbers "c(1, 2)"; applies to all excel sheets. Passed to e_read_data_files().

sw_clean_names

For data, T/F to clean column names using janitor::clean_names

sw_list_or_flat

Hierarical list or a "flat" 1-level list (if "fn" with "flat", then will prepend path to fn)

excel_range

When reading Excel files, NULL reads entire sheet, a range is specified as in readxl::read_xlsx. Applies to all files.

excel_col_names

Specified as in readxl::read_xlsx. Applies to all files.

sw_delim

F if standard delim, otherwise delim character such as "|"

sw_read_package_csv_txt

"readr" for read_csv and read_delim, "utils" for read.csv and read.delim

Value

fn_names Either a structured list of filenames or of tibbles

Examples

## Not run: 
# # all file names
# e_read_data_subdir_into_lists(
#     fn_path   = "./data-raw/dat_subdir"
#   , fn_detect = NULL
#   , sw_fn_or_dat  = c("fn", "dat")[1]
#   , sw_exclude_empty_dir = c(TRUE, FALSE)[1]
#   )
# # selected file names
# e_read_data_subdir_into_lists(
#     fn_path   = "./data-raw/dat_subdir"
#   , fn_detect = c("csv$", "xls$", "xlsx$")
#   , sw_fn_or_dat  = c("fn", "dat")[1]
#   , sw_exclude_empty_dir = c(TRUE, FALSE)[1]
#   )
# # selected data
# e_read_data_subdir_into_lists(
#     fn_path                 = "./data-raw/dat_subdir"
#   , fn_detect               = c("csv$", "xls$", "xlsx$")
#   , sw_fn_or_dat            = c("fn", "dat")[2]
#   , sw_exclude_empty_dir    = c(TRUE, FALSE)[1]
#   , sw_dat_add_col_path_fn  = c(TRUE, FALSE)[1]
#   , sw_dat_print_fn_read    = c(TRUE, FALSE)[1]
#   , sw_clean_names          = c(TRUE, FALSE)[2]
#   , sw_list_or_flat         = c("list", "flat")[1]
#   , sw_delim                = c(FALSE, "|")[1]
#   , sw_read_package_csv_txt = c("readr", "utils")[1]
#   )
# # selected data, flatten the directory structure
# e_read_data_subdir_into_lists(
#     fn_path                 = "./data-raw/dat_subdir"
#   , fn_detect               = c("csv$", "xls$", "xlsx$")
#   , sw_fn_or_dat            = c("fn", "dat")[2]
#   , sw_exclude_empty_dir    = c(TRUE, FALSE)[1]
#   , sw_dat_add_col_path_fn  = c(TRUE, FALSE)[1]
#   , sw_dat_print_fn_read    = c(TRUE, FALSE)[1]
#   , sw_clean_names          = c(TRUE, FALSE)[2]
#   , sw_list_or_flat         = c("list", "flat")[2]
#   , sw_delim                = c(FALSE, "|")[1]
#   , sw_read_package_csv_txt = c("readr", "utils")[1]
#   )

## End(Not run)

erikerhardt/erikmisc documentation built on April 17, 2025, 10:48 a.m.