The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

tor

lifecycle Travis build status Codecov test coverage Coverage status CRAN status

tor (to-R) helps you to import multiple files at once. For example:

Installation

Install tor from CRAN with:

install.packages("tor")

Or install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("maurolepore/tor")

Example

library(tor)

list_*(): Import multiple files from a directory into a list

All functions default to importing files from the working directory.

dir()
#>  [1] "_pkgdown.yml"     "codecov.yml"      "cran-comments.md" "csv1.csv"        
#>  [5] "csv2.csv"         "DESCRIPTION"      "inst"             "LICENSE.md"      
#>  [9] "man"              "NAMESPACE"        "NEWS.md"          "R"               
#> [13] "README.md"        "README.Rmd"       "tests"            "tor.Rproj"       
#> [17] "vignettes"

list_csv()
#> Parsed with column specification:
#> cols(
#>   x = col_double()
#> )
#> Parsed with column specification:
#> cols(
#>   y = col_character()
#> )
#> $csv1
#> # A tibble: 2 x 1
#>       x
#>   <dbl>
#> 1     1
#> 2     2
#> 
#> $csv2
#> # A tibble: 2 x 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

Often you will specify a path to read from.

# Helpes create paths to examples
tor_example()
#> [1] "csv"   "mixed" "rdata" "rds"   "tsv"

(path_rds <- tor_example("rds"))
#> [1] "/home/mauro/R/x86_64-pc-linux-gnu-library/3.6/tor/extdata/rds"
dir(path_rds)
#> [1] "rds1.rds" "rds2.rds"

list_rds(path_rds)
#> $rds1
#> # A tibble: 2 x 1
#>       x
#>   <dbl>
#> 1     1
#> 2     2
#> 
#> $rds2
#> # A tibble: 2 x 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

You may read all files with a particular extension.

path_mixed <- tor_example("mixed")
dir(path_mixed)
#> [1] "csv.csv"           "lower_rdata.rdata" "rda.rda"          
#> [4] "upper_rdata.RData"

list_rdata(path_mixed)
#> $lower_rdata
#> # A tibble: 2 x 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#> 
#> $rda
#> # A tibble: 2 x 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b    
#> 
#> $upper_rdata
#> # A tibble: 2 x 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

Or you may read specific files matching a pattern.

list_rdata(path_mixed, regexp = "[.]RData", ignore.case = FALSE)
#> $upper_rdata
#> # A tibble: 2 x 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

list_any() is the most flexible function. You supply the function to read with.

(path_csv <- tor_example("csv"))
#> [1] "/home/mauro/R/x86_64-pc-linux-gnu-library/3.6/tor/extdata/csv"
dir(path_csv)
#> [1] "csv1.csv" "csv2.csv"

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

It understands lambda functions and formulas (powered by rlang).

# Use the pipe (%>%)
library(magrittr)

(path_rdata <- tor_example("rdata"))
#> [1] "/home/mauro/R/x86_64-pc-linux-gnu-library/3.6/tor/extdata/rdata"
dir(path_rdata)
#> [1] "rdata1.rdata" "rdata2.rdata"

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

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

Pass additional arguments via ... or inside the lambda function.

path_csv %>% 
  list_any(readr::read_csv, skip = 1)
#> Parsed with column specification:
#> cols(
#>   `1` = col_double()
#> )
#> Parsed with column specification:
#> cols(
#>   a = col_character()
#> )
#> $csv1
#> # A tibble: 1 x 1
#>     `1`
#>   <dbl>
#> 1     2
#> 
#> $csv2
#> # A tibble: 1 x 1
#>   a    
#>   <chr>
#> 1 b

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

It also provides the arguments regexp, ignore.case, and invert to pick specific files in a directory (powered by fs).

path_mixed <- tor_example("mixed")
dir(path_mixed)
#> [1] "csv.csv"           "lower_rdata.rdata" "rda.rda"          
#> [4] "upper_rdata.RData"

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

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

load_*(): Load multiple files from a directory into an environment

All functions default to importing files from the working directory and into the global environment.

# The working directory contains .csv files
dir()
#>  [1] "_pkgdown.yml"     "codecov.yml"      "cran-comments.md" "csv1.csv"        
#>  [5] "csv2.csv"         "DESCRIPTION"      "inst"             "LICENSE.md"      
#>  [9] "man"              "NAMESPACE"        "NEWS.md"          "R"               
#> [13] "README.md"        "README.Rmd"       "tests"            "tor.Rproj"       
#> [17] "vignettes"

load_csv()
#> Parsed with column specification:
#> cols(
#>   x = col_double()
#> )
#> Parsed with column specification:
#> cols(
#>   y = col_character()
#> )

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

rm(list = ls())

You may import files from a specific path.

(path_mixed <- tor_example("mixed"))
#> [1] "/home/mauro/R/x86_64-pc-linux-gnu-library/3.6/tor/extdata/mixed"
dir(path_mixed)
#> [1] "csv.csv"           "lower_rdata.rdata" "rda.rda"          
#> [4] "upper_rdata.RData"

load_rdata(path_mixed)

ls()
#> [1] "lower_rdata" "path_mixed"  "rda"         "upper_rdata"
rda
#> # A tibble: 2 x 1
#>   y    
#>   <chr>
#> 1 a    
#> 2 b

You may import files into a specific environment.

e <- new.env()
ls(e)
#> character(0)

load_rdata(path_mixed, envir = e)

ls(e)
#> [1] "lower_rdata" "rda"         "upper_rdata"

For more flexibility use load_any() with a function able to read one file of the format you want to import.

dir()
#>  [1] "_pkgdown.yml"     "codecov.yml"      "cran-comments.md" "csv1.csv"        
#>  [5] "csv2.csv"         "DESCRIPTION"      "inst"             "LICENSE.md"      
#>  [9] "man"              "NAMESPACE"        "NEWS.md"          "R"               
#> [13] "README.md"        "README.Rmd"       "tests"            "tor.Rproj"       
#> [17] "vignettes"

load_any(".", .f = readr::read_csv, regexp = "[.]csv$")
#> Parsed with column specification:
#> cols(
#>   x = col_double()
#> )
#> Parsed with column specification:
#> cols(
#>   y = col_character()
#> )

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

Related projects

Two great packages to read and write data are rio and io.

Information

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.