Skip to content

Commit

Permalink
Refactor (#1)
Browse files Browse the repository at this point in the history
* Refine doc.
* Refactor list_rds(), list_rdata(), list_csv() and list_tsv():
* New argument `regexp`
* New argument `invert`
* New argument `ignore.case`
* Reorder arguments for consistency.
  • Loading branch information
maurolepore committed Jan 3, 2019
1 parent 8c2b714 commit 4cd18d3
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 116 deletions.
57 changes: 39 additions & 18 deletions R/list_rds.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,40 @@
#' dir(tsv)
#'
#' list_tsv(tsv)
list_rds <- function(path = ".") {
list_any(path, base::readRDS, regexp = "[.]rds$", ignore.case = TRUE)
list_rds <- function(path = ".",
regexp = "[.]rds$",
ignore.case = TRUE,
invert = FALSE) {
list_any(
path,
base::readRDS,
regexp = regexp,
ignore.case = ignore.case,
invert = invert
)
}

#' @rdname list_rds
#' @export
list_rdata <- function(path = ".") {
list_rdata <- function(path = ".",
regexp = "[.]rdata$|[.]rda$",
ignore.case = TRUE,
invert = FALSE) {
list_any(
path,
~get(load(.x)),
regexp = "[.]rdata$|[.]rda$",
ignore.case = TRUE
regexp = regexp,
ignore.case = ignore.case,
invert = invert
)
}

#' @rdname list_rds
#' @export
list_csv <- function(path = ".",
regexp = "[.]csv$",
ignore.case = TRUE,
invert = FALSE,
header = TRUE,
sep = ",",
quote = "\"",
Expand All @@ -68,24 +84,28 @@ list_csv <- function(path = ".",
stringsAsFactors = stringsAsFactors,
na.strings = na.strings
),
regexp = "[.]csv$",
ignore.case = TRUE,
regexp = regexp,
ignore.case = ignore.case,
invert = invert,
...
)
}

#' @rdname list_rds
#' @export
list_tsv <- function(path = ".",
header = TRUE,
sep = "\t",
quote = "\"",
dec = ".",
fill = TRUE,
comment.char = "",
stringsAsFactors = FALSE,
na.strings = c("", "NA"),
...) {
regexp = "[.]tsv$",
ignore.case = TRUE,
invert = FALSE,
header = TRUE,
sep = "\t",
quote = "\"",
dec = ".",
fill = TRUE,
comment.char = "",
stringsAsFactors = FALSE,
na.strings = c("", "NA"),
...) {
list_any(
path,
~utils::read.csv(
Expand All @@ -99,8 +119,9 @@ list_tsv <- function(path = ".",
stringsAsFactors = stringsAsFactors,
na.strings = na.strings
),
regexp = "[.]tsv$",
ignore.case = TRUE,
regexp = regexp,
ignore.case = ignore.case,
invert = invert,
...
)
}
Empty file added R/rds_list.R
Empty file.
18 changes: 7 additions & 11 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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__ is to import multiple files of any kind into R. It does nothing you can't do with functions from base R (or [__fs__](https://fs.r-lib.org/) plus [__purrr__](https://purrr.tidyverse.org/ plus some reader package)) but it provides a shortcut to save your time and brain power for more important tasks. __tor__ is flexible and small, and works with tools from the [tidyverse](https://www.tidyverse.org/).
The goal of __tor__ (_to-R_) is to import multiple files of any kind into R. It does nothing you can't do with functions from base R (or [__fs__](https://fs.r-lib.org/) plus [__purrr__](https://purrr.tidyverse.org/) plus some reader package) but it provides a shortcut to save your time and brain power for more important tasks. __tor__ is flexible and small, and works well with tools from the [tidyverse](https://www.tidyverse.org/).

## Installation

Expand All @@ -34,7 +34,7 @@ devtools::install_github("maurolepore/tor")
## Example

```{r}
library(magrittr)
library(purrr)
library(fs)
library(tor)
```
Expand Down Expand Up @@ -71,7 +71,7 @@ list_csv(path_mixed)
list_rdata(path_mixed)
```

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

```{r}
(path_csv <- tor_example("csv"))
Expand All @@ -94,7 +94,7 @@ path_rdata %>%
list_any(~get(load(.x)))
```

Pass additional arguments via `...` or inside the lambda function (as `lapply()`).
Pass additional arguments via `...` or inside the lambda function.

```{r}
list_any(path_csv, read.csv, stringsAsFactors = FALSE)
Expand All @@ -117,9 +117,7 @@ path_mixed %>%

### Writing data

__tor__ does not write data. Compared to reading, writing data is a little easier because you have all the tools from R to choose what to write, how and where.

Yet __tor__ helps you in a small, important way. Because creating the paths to write files may interrupt your workflow, __torr__ provides a helper to do just that.
__tor__ does not write data but includes a helper to create the paths to output files.

```{r}
dir(pattern = "[.]csv$")
Expand All @@ -128,14 +126,12 @@ dfms <- list_csv()
format_path(names(dfms), "csv")
format_path(names(dfms), "csv", "base", "prefix-")
format_path(names(dfms), "csv", base = "home", prefix = "this-")
```

Combine it with [__purrr__](https://purrr.tidyverse.org/.
Combine it with [__purrr__](https://purrr.tidyverse.org/).

```{r}
library(purrr)
imap_chr(dfms, ~ format_path(.y, "csv"))
# Same
Expand Down
42 changes: 15 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ status](https://coveralls.io/repos/github/maurolepore/tor/badge.svg)](https://co
[![CRAN
status](https://www.r-pkg.org/badges/version/tor)](https://cran.r-project.org/package=tor)

The goal of **tor** is to import multiple files of any kind into R. It
does nothing you can’t do with functions from base R (or
The goal of **tor** (*to-R*) is to import multiple files of any kind
into R. It does nothing you can’t do with functions from base R (or
[**fs**](https://fs.r-lib.org/) plus
[**purrr**](https://purrr.tidyverse.org/%20plus%20some%20reader%20package))
but it provides a shortcut to save your time and brain power for more
important tasks. **tor** is flexible and small, and works with tools
from the [tidyverse](https://www.tidyverse.org/).
[**purrr**](https://purrr.tidyverse.org/) plus some reader package) but
it provides a shortcut to save your time and brain power for more
important tasks. **tor** is flexible and small, and works well with
tools from the [tidyverse](https://www.tidyverse.org/).

## Installation

Expand All @@ -29,7 +29,7 @@ devtools::install_github("maurolepore/tor")
## Example

``` r
library(magrittr)
library(purrr)
library(fs)
library(tor)
```
Expand Down Expand Up @@ -127,7 +127,8 @@ list_rdata(path_mixed)
#> 2 b
```

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

``` r
(path_csv <- tor_example("csv"))
Expand Down Expand Up @@ -182,8 +183,7 @@ path_rdata %>%
#> 2 b
```

Pass additional arguments via `...` or inside the lambda function (as
`lapply()`).
Pass additional arguments via `...` or inside the lambda function.

``` r
list_any(path_csv, read.csv, stringsAsFactors = FALSE)
Expand Down Expand Up @@ -250,13 +250,8 @@ path_mixed %>%

### Writing data

**tor** does not write data. Compared to reading, writing data is a
little easier because you have all the tools from R to choose what to
write, how and where.

Yet **tor** helps you in a small, important way. Because creating the
paths to write files may interrupt your workflow, **torr** provides a
helper to do just that.
**tor** does not write data but includes a helper to create the paths to
output files.

``` r
dir(pattern = "[.]csv$")
Expand All @@ -267,20 +262,13 @@ dfms <- list_csv()
format_path(names(dfms), "csv")
#> [1] "./csv1.csv" "./csv2.csv"

format_path(names(dfms), "csv", "base", "prefix-")
#> [1] "base/prefix-csv1.csv" "base/prefix-csv2.csv"
format_path(names(dfms), "csv", base = "home", prefix = "this-")
#> [1] "home/this-csv1.csv" "home/this-csv2.csv"
```

Combine it with \[**purrr**\](<https://purrr.tidyverse.org/>.
Combine it with [**purrr**](https://purrr.tidyverse.org/).

``` r
library(purrr)
#>
#> Attaching package: 'purrr'
#> The following object is masked from 'package:magrittr':
#>
#> set_names

imap_chr(dfms, ~ format_path(.y, "csv"))
#> csv1 csv2
#> "./csv1.csv" "./csv2.csv"
Expand Down
19 changes: 15 additions & 4 deletions man/list_rds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Loading

0 comments on commit 4cd18d3

Please sign in to comment.