knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(erikmisc)

Show example files in subdirectories

This is the directory tree with files that are being recursed.

if (.Platform$OS.type == "windows") {
  system(
      command = "tree ..\\data-raw\\dat_subdir /f"
    , intern  = TRUE
    )
}

Read and return filenames in list

It's good to clarify the files before reading them.

This includes all files and empty directories.

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)[2]
  )

This selects specified file extensions and excludes empty directories.

# 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]
  )

Read and return data in list

Now that we're happy with the files to read, we read the files, including the path and filename as additional columns in each file, and printing the filename path and dimension during the process.

Note that all of these example files have the same content, columns A, B, and C.

# selected data
dat_sheet <-
  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]
  )

dat_sheet |> print()

Finally, files within a folder with no other subdirectories can easily be bind'ed together.

dplyr::bind_rows(dat_sheet$dir_b)


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