Skip to content

Commit

Permalink
Use readr.
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore committed Jan 8, 2019
1 parent 1f65d46 commit b4d9522
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 182 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ BugReports: https://github.com/maurolepore/tor/issues
Imports:
fs,
rlang,
tibble
tibble,
readr
Suggests:
covr,
spelling,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Minor

* `list_any()`, `list_csv()`, `list_tsv()`, `load_csv()`, and `load_tsv()` now use __readr__.
* This makes reading data faster and safer.

* Each dataframe output is now converted to tibble.
* Users no longer need to call `tibble::as_tibble()`.

Expand Down
4 changes: 1 addition & 3 deletions R/list_any.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ list_any <- function(path = ".",
)

if (length(files) == 0) {
abort(
sprintf("Can't find files matching '%s' in:\n '%s'", regexp, path)
)
abort(sprintf("Can't find files matching '%s' in:\n '%s'", regexp, path))
}

file_names <- fs::path_ext_remove(fs::path_file(files))
Expand Down
44 changes: 4 additions & 40 deletions R/list_csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' These functions wrap the most common special cases of [list_any()].
#'
#' @inheritParams list_any
#' @inheritParams utils::read.table
#' @param ... Arguments passed to [utils::read.table()].
#' @inheritParams readr::read_delim
#' @param ... Arguments passed to `readr::read_csv()` or `readr::read_tsv()`.
#'
#' @return A list.
#'
Expand Down Expand Up @@ -33,28 +33,10 @@ list_csv <- function(path = ".",
regexp = "[.]csv$",
ignore.case = TRUE,
invert = FALSE,
header = TRUE,
sep = ",",
quote = "\"",
dec = ".",
fill = TRUE,
comment.char = "",
stringsAsFactors = FALSE,
na.strings = c("", "NA"),
...) {
list_any(
path,
function(x) utils::read.csv(
file = x,
header = header,
sep = sep,
quote = quote,
dec = dec,
fill = fill,
comment.char = comment.char,
stringsAsFactors = stringsAsFactors,
na.strings = na.strings
),
readr::read_csv,
regexp = regexp,
ignore.case = ignore.case,
invert = invert,
Expand All @@ -68,28 +50,10 @@ list_tsv <- function(path = ".",
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,
function(x) utils::read.csv(
file = x,
header = header,
sep = sep,
quote = quote,
dec = dec,
fill = fill,
comment.char = comment.char,
stringsAsFactors = stringsAsFactors,
na.strings = na.strings
),
.f = readr::read_tsv,
regexp = regexp,
ignore.case = ignore.case,
invert = invert,
Expand Down
41 changes: 2 additions & 39 deletions R/load_csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,11 @@ load_csv <- function(path = ".",
regexp = "[.]csv$",
ignore.case = TRUE,
invert = FALSE,
header = TRUE,
sep = ",",
quote = "\"",
dec = ".",
fill = TRUE,
comment.char = "",
stringsAsFactors = FALSE,
na.strings = c("", "NA"),
envir = .GlobalEnv,
...) {
lst <- list_any(
path,
# TODO: Use function(x) to more obviously show what's going on
function(x) utils::read.csv(
file = x,
header = header,
sep = sep,
quote = quote,
dec = dec,
fill = fill,
comment.char = comment.char,
stringsAsFactors = stringsAsFactors,
na.strings = na.strings
),
readr::read_csv,
regexp = regexp,
ignore.case = ignore.case,
invert = invert,
Expand All @@ -67,29 +48,11 @@ load_tsv <- function(path = ".",
regexp = "[.]tsv$",
ignore.case = TRUE,
invert = FALSE,
header = TRUE,
sep = "\t",
quote = "\"",
dec = ".",
fill = TRUE,
comment.char = "",
stringsAsFactors = FALSE,
na.strings = c("", "NA"),
envir = .GlobalEnv,
...) {
lst <- list_any(
path,
function(x) utils::read.csv(
file = x,
header = header,
sep = sep,
quote = quote,
dec = dec,
fill = fill,
comment.char = comment.char,
stringsAsFactors = stringsAsFactors,
na.strings = na.strings
),
readr::read_tsv,
regexp = regexp,
ignore.case = ignore.case,
invert = invert,
Expand Down
2 changes: 1 addition & 1 deletion inst/extdata/tsv/tsv1.tsv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
x y
1 a
2
2 NA
3 NA
2 changes: 1 addition & 1 deletion inst/extdata/tsv/tsv2.tsv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
x y
1 a
2
2 NA
3 b
47 changes: 3 additions & 44 deletions man/list_csv.Rd

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

49 changes: 3 additions & 46 deletions man/load_csv.Rd

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

File renamed without changes.
3 changes: 3 additions & 0 deletions tests/testthat/csv2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
y
a
b
3 changes: 0 additions & 3 deletions tests/testthat/file1.csv

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testthat/file2.csv

This file was deleted.

3 changes: 2 additions & 1 deletion tests/testthat/test-list_csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ test_that("list_csv defaults to `stringsAsFactors = FALSE`", {
})

test_that("list_csv defaults to read from working directory", {
expect_true(any("csv" %in% names(list_csv())))
# WARNING: Not for interactive use.
expect_true(all(c("csv1", "csv2") %in% names(list_csv())))
})

test_that("list_csv is sensitive to `regexp`, `invert, and `ignore.case`", {
Expand Down

0 comments on commit b4d9522

Please sign in to comment.