Skip to contents

Import multiple files of any format from a directory into a list.

Usage

list_any(
  path = ".",
  .f,
  regexp = NULL,
  ignore.case = FALSE,
  invert = FALSE,
  ...
)

Arguments

path

A character vector of one path. Defaults to the working directory.

.f

A function able to read the desired file format.

regexp

A regular expression (e.g. [.]csv$) passed on to grep() to filter paths.

ignore.case

if FALSE, the pattern matching is case sensitive and if TRUE, case is ignored during matching.

invert

If TRUE return files which do not match

...

Additional arguments passed to .f.

Value

A list.

See also

Other functions to import files into a list: list_csv()

Other functions to import files of any format: load_any()

Examples

tor_example()
#> [1] "csv"   "mixed" "rdata" "rds"   "tsv"  

(path <- tor_example("csv"))
#> [1] "/home/runner/work/_temp/Library/tor/extdata/csv"
dir(path)
#> [1] "csv1.csv" "csv2.csv"

list_any(path, read.csv)
#> $csv1
#> # A tibble: 2 × 1
#>       x
#>   <int>
#> 1     1
#> 2     2
#> 
#> $csv2
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#> 

list_any(path, ~ read.csv(.x, stringsAsFactors = FALSE))
#> $csv1
#> # A tibble: 2 × 1
#>       x
#>   <int>
#> 1     1
#> 2     2
#> 
#> $csv2
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#> 

(path_mixed <- tor_example("mixed"))
#> [1] "/home/runner/work/_temp/Library/tor/extdata/mixed"
dir(path_mixed)
#> [1] "csv.csv"           "lower_rdata.rdata" "rda.rda"          
#> [4] "upper_rdata.RData"

list_any(
  path_mixed, ~ get(load(.x)),
  "[.]Rdata$",
  ignore.case = TRUE
)
#> $lower_rdata
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#> 
#> $upper_rdata
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#> 

list_any(
  path_mixed, ~ get(load(.x)),
  regexp = "[.]csv$",
  invert = TRUE
)
#> $lower_rdata
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#> 
#> $rda
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#> 
#> $upper_rdata
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#>