read_dir_csv: Read CSV files form a directory

Description Usage Arguments Value See Also Examples

View source: R/read_dir_csv.R

Description

Read multiple CSV files from a directory (folder)

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
read_dir_csv(
  path = ".",
  ...,
  all = F,
  recurse = F,
  strict_csv_ext = T,
  regexp = ".",
  invert = F,
  ignore.case = F,
  perl = F,
  fixed = F,
  snake_case = F
)

Arguments

path

Character: Path to desired directory which contain file, default is "." working directory. (passed to fs::dir_ls())

...

Argument to pass to readr::read_csv()

all

If TRUE hidden files are also returned. (passed to fs::dir_ls())

recurse

If TRUE read recursively in sub-directory, if a positive number the number of levels to recurse. (passed to fs::dir_ls())

strict_csv_ext

If TRUE (default): Strictly read only files, which has files name ending with .csv

regexp

Character: Specify a regular expression to read from file names (passed to grep()) . If you want to match file names other than .csv, turn strict_csv_ext = FALSE.

invert

Logical: If TRUE read from file that file names do not match regexp.(passed to grep())

ignore.case

If FALSE, the regular expression matching is case sensitive and if TRUE, case is ignored during matching.(passed to grep())

perl

Logical: Should Perl-compatible regexps be used? (passed to grep())

fixed

Logical: If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.

snake_case

If TRUE: Convert data frame names to snake_case_format. (require snakecase package)

Value

A list of tibbles

See Also

readr::read_csv() https://readr.tidyverse.org

fs package https://fs.r-lib.org/index.html

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#' # Not run
if(FALSE){
# Read form current working directory by default
  read_dir_csv()
# And file names are set to names of each data frame in a list

# Give a directory path
  read_dir_csv("path/to/dir")

# Also, can read from every sub-directory of given directory
  read_dir_csv("path/to/dir", recurse = T)

# Can specify regular expression of file names to read.
  read_dir_csv(regexp = "[[:digit:]]+")  # CSV file name contains numbers

# If set strict_csv_ext = F and remove regex which require file name ending with .csv
# it might read other file type as well (if you like)
  read_dir_csv(regexp = "[[:digit:]]+\\.txt$", strict_csv_ext = FALSE) # Try read .txt file

# If you want file names in snake_case (require snakecase package)
  read_dir_csv(snake_case = TRUE)
}

Lightbridge-AI/lbr documentation built on Dec. 27, 2021, 8:09 p.m.