Skip to content

Commit

Permalink
Merge 431d4c2 into c9a6c9c
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore committed Jan 14, 2019
2 parents c9a6c9c + 431d4c2 commit eaa055f
Show file tree
Hide file tree
Showing 34 changed files with 1,446 additions and 500 deletions.
89 changes: 0 additions & 89 deletions .buildignore/tor-1-0-0.Rmd

This file was deleted.

Binary file removed .buildignore/tor_1.0.0.zip
Binary file not shown.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tor
Title: Import Multiple Files From a Single Directory at Once
Version: 1.0.1.9000
Version: 1.0.1
Authors@R:
person(given = "Mauro",
family = "Lepore",
Expand All @@ -24,6 +24,8 @@ Suggests:
rmarkdown,
spelling,
testthat
VignetteBuilder:
knitr
Encoding: UTF-8
Language: en-US
LazyData: true
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(list_csv)
export(list_rdata)
export(list_rds)
export(list_tsv)
export(load_any)
export(load_csv)
export(load_rdata)
export(load_rds)
Expand Down
10 changes: 6 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# tor 1.0.1.9000
# tor (development version)

* Work in progress.
* In `list_any()` `...` is now correctly defined.
* New `load_any()` is analog to `list_any()` and completes the set.
* The title of `load_csv()` and friends is now correct.

# tor 1.0.1 (GitHub release 2019-01-09)
# tor 1.0.1 (CRAN release)

* `list_any()`, `list_csv()`, `list_tsv()`, `load_csv()`, and `load_tsv()` now use __readr__.
* This makes reading data faster and safer.
Expand All @@ -15,6 +17,6 @@
* `dir(tor_example("rds"))` returns `c("rds1.rds", "rds2.rds")`.
* `dir(tor_example("csv"))` returns `c("csv1.csv", "csv2.csv")`.

# tor 1.0.0 (GitHub release 2019-01-07)
# tor 1.0.0 (GitHub release)

* Initial GitHub release.
21 changes: 12 additions & 9 deletions R/list_any.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
#' Read multiple files into a list with your favorite reader function.
#' Import multiple files of any format from a directory into a list.
#'
#' @param path A character vector of one path.
#' @param .f A function able to read the desired file format.
#' @inheritParams fs::dir_ls
#' @inheritParams base::grep
#' @param .f A function able to read the desired file format.
#' @param ... Additional arguments passed to `.f`.
#'
#' @return A list.
#'
#' @examples
#' tor_example()
#'
#'
#' path <- tor_example("csv")
#' dir(path)
#'
#'
#' list_any(path, read.csv)
#'
#'
#' list_any(path, ~ read.csv(.x, stringsAsFactors = FALSE))
#'
#'
#' (path_mixed <- tor_example("mixed"))
#' dir(path_mixed)
#'
#'
#' list_any(
#' path_mixed, ~ get(load(.x)),
#' regexp = "[.]csv$",
#' invert = TRUE
#' )
#'
#'
#' list_any(
#' path_mixed, ~ get(load(.x)),
#' "[.]Rdata$",
#' ignore.case = TRUE
#' )
#' @family general functions to import data
#' @family functions to import files into a list
#' @family functions to import files of any format
#' @export
list_any <- function(path = ".",
.f,
Expand Down
19 changes: 10 additions & 9 deletions R/list_csv.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Read multiple files from a directory into a list.
#' Import multiple common files from a directory into a list.
#'
#' These functions wrap the most common special cases of [list_any()].
#'
Expand All @@ -11,23 +11,24 @@
#' @examples
#' (rds <- tor_example("rds"))
#' dir(rds)
#'
#'
#' list_rds(rds)
#'
#'
#' (tsv <- tor_example("tsv"))
#' dir(tsv)
#'
#'
#' list_tsv(tsv)
#'
#'
#' (mixed <- tor_example("mixed"))
#' dir(mixed)
#'
#'
#' list_rdata(mixed)
#'
#'
#' list_csv(mixed)
#'
#'
#' list_rdata(mixed, regexp = "[.]RData", ignore.case = FALSE)
#' @family general functions to import data
#' @family functions to import files into a list
#' @family functions to import files of common formats
#' @export
list_csv <- function(path = ".",
regexp = "[.]csv$",
Expand Down
41 changes: 41 additions & 0 deletions R/load_any.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' Import multiple files of any format from a directory into an environment.
#'
#' @param path A character vector of one path.
#' @param .f A function able to read the desired file format.
#' @inheritParams fs::dir_ls
#' @inheritParams base::grep
#' @inheritParams base::list2env
#' @param ... Additional arguments passed to `.f`.
#'
#' @return `invisible(path)`.
#' @export
#'
#' @examples
#' e <- new.env()
#' load_any(tor_example("rdata"), .f = ~ get(load(.x)), envir = e)
#' ls(e)
#'
#' # The data is now available in the environment `e`
#' e$rdata1
#' e$rdata2
#' @family functions to import files into an environment
#' @family functions to import files of any format
load_any <- function(path = ".",
.f,
regexp = NULL,
ignore.case = FALSE,
invert = FALSE,
envir = .GlobalEnv,
...) {
lst <- list_any(
path = path,
.f = .f,
regexp = regexp,
ignore.case = ignore.case,
invert = invert,
...
)

list2env(lst, envir = envir)
invisible(path)
}
17 changes: 10 additions & 7 deletions R/load_csv.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#' Load each element of a list into an environment.
#' Import multiple common files from a directory into an environment.
#'
#' @inheritParams list_csv
#' @inheritParams base::list2env
#' @inheritParams load_any
#' @inheritParams readr::read_delim
#' @param ... Arguments passed to `readr::read_csv()` or `readr::read_tsv()`.
#'
#' @return `invisible(path)`.
#'
#' @examples
#' (path_csv <- tor_example("csv"))
#' dir(path_csv)
#'
#'
#' load_csv(path_csv)
#' # Each dataframe is now available in the global environment
#' csv1
#' csv2
#'
#'
#' (path_mixed <- tor_example("mixed"))
#' dir(path_mixed)
#'
#'
#' load_rdata(path_mixed)
#' # Each dataframe is now available in the global environment
#' lower_rdata
#' upper_rdata
#' @family general functions to import data
#' @family functions to import files into an environment
#' @family functions to import files of common formats
#' @export
load_csv <- function(path = ".",
regexp = "[.]csv$",
Expand Down Expand Up @@ -100,3 +102,4 @@ load_rdata <- function(path = ".",
list2env(lst, envir = envir)
invisible(path)
}

23 changes: 21 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ knitr::opts_chunk$set(
[![Coverage status](https://coveralls.io/repos/github/maurolepore/tor/badge.svg)](https://coveralls.io/r/maurolepore/tor?branch=master)
[![CRAN status](https://www.r-pkg.org/badges/version/tor)](https://cran.r-project.org/package=tor)

The goal of __tor__ (_to-R_) is to make importing data into R ridiculously easy. It helps you to import multiple files from a single directory into R in a simple, intuitive way. `list_csv()` creates a list of all "\*.csv" files in your working directory; and `load_rdata()` loads all "\*.rdata" files into your global environment (see all functions in [Reference](https://maurolepore.github.io/tor/reference/index.html)). __tor__ does nothing you can't do with functions from base R but it makes a frequent task less painful. It has few dependencies and works well with the [tidyverse](https://www.tidyverse.org/).
The goal of __tor__ (_to-R_) is to make importing data into R ridiculously easy. It helps you to import multiple files from a single directory into R in a simple, intuitive way. For example, `load_csv()` with no argument loads all "\*.csv" files in your working directory into the global environment, making them directly available for analysis.

## Installation

Install __tor__ from CRAN with:

```r
install.packages("tor")
```

Or install the development version from GitHub with:

``` r
# install.packages("devtools")
devtools::install_github("maurolepore/tor")
Expand Down Expand Up @@ -125,7 +133,7 @@ path_mixed %>%

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

All functions default to load from the working directory.
All functions default to loading from the working directory.

```{r}
# The working directory contains .csv files
Expand Down Expand Up @@ -153,6 +161,17 @@ ls()
rda
```

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

```{r}
dir()
load_any(".", .f = readr::read_csv, regexp = "[.]csv$")
# The data is now available in the global environment
csv1
csv2
```

# Related projects

Expand Down
Loading

0 comments on commit eaa055f

Please sign in to comment.