Introduction

I’m happy to announce that tor 1.1.1 in now on CRAN! tor (to-R) helps you to import multiple files at once into a list or an environment. It makes a small but frequent task less painful.

You can install tor with:

install.packages("tor")

List common files

list_csv() and friends import multiple common files from a directory into a list. They default to importing from the working directory.

dir()
#> [1] "csv1.csv"      "csv2.csv"      "tor-1-1-1.Rmd"

list_csv()
#> Rows: 2 Columns: 1
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (1): x
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> Rows: 2 Columns: 1
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): y
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> $csv1
#> # A tibble: 2 × 1
#>       x
#>   <dbl>
#> 1     1
#> 2     2
#> 
#> $csv2
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

You can specify a path to import from any directory. regexp, ignore.case, and invert allow you to pick specific files.

(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_rdata(
  path_mixed, 
  regexp = "[.]RData", 
  ignore.case = FALSE, 
  invert = FALSE
)
#> $upper_rdata
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

Load common files

load_csv() and friends import files into an environment – the global environment by default.

dir()
#> [1] "csv1.csv"      "csv2.csv"      "tor-1-1-1.Rmd"

load_csv()
#> Rows: 2 Columns: 1
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (1): x
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> Rows: 2 Columns: 1
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): y
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

# Each dataframe is now available in the global environment
csv1
#> # A tibble: 2 × 1
#>       x
#>   <dbl>
#> 1     1
#> 2     2
csv2
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

Beyond common files

list_any() imports files of any format into a list. You need to supply a function able to import a single file.

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

list_any(path_csv, readr::read_csv)
#> Rows: 2 Columns: 1
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (1): x
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> Rows: 2 Columns: 1
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): y
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> $csv1
#> # A tibble: 2 × 1
#>       x
#>   <dbl>
#> 1     1
#> 2     2
#> 
#> $csv2
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

You may use anonymous functions using the formula syntax (powered by rlang).

(path_rdata <- tor_example("rdata"))
#> [1] "/home/runner/work/_temp/Library/tor/extdata/rdata"
dir(path_rdata)
#> [1] "rdata1.rdata" "rdata2.rdata"

path_rdata %>% 
  list_any(function(x) get(load(x)))
#> $rdata1
#> # A tibble: 2 × 1
#>       x
#>   <dbl>
#> 1     1
#> 2     2
#> 
#> $rdata2
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

# Same
path_rdata %>% 
  list_any(~get(load(.x)))
#> $rdata1
#> # A tibble: 2 × 1
#>       x
#>   <dbl>
#> 1     1
#> 2     2
#> 
#> $rdata2
#> # A tibble: 2 × 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b