Skip to content

Commit

Permalink
Refactor list_csv() and list_tsv().
Browse files Browse the repository at this point in the history
* Reorder arguments for consistency.
New arguments:
* `regexp`
* `ignore.case`
* `invert`
  • Loading branch information
maurolepore committed Jan 3, 2019
1 parent 46662e9 commit e989bd5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
34 changes: 21 additions & 13 deletions R/list_rds.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ list_rdata <- function(path = ".",
#' @rdname list_rds
#' @export
list_csv <- function(path = ".",
regexp = "[.]csv$",
ignore.case = TRUE,
invert = FALSE,
header = TRUE,
sep = ",",
quote = "\"",
Expand All @@ -81,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 @@ -112,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,
...
)
}
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.

67 changes: 58 additions & 9 deletions tests/testthat/test-list_rds.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ test_that("list_rdata reads specific .rdata files (sensitive to `regexp`)", {
})

test_that("list_rdata is sensitive to `ignore.case`", {
expect_named(
list_rdata(
tor_example("mixed"),
regexp = "LOWER_rdata|[.]csv$",
ignore.case = TRUE,
invert = TRUE
),
c("rda", "upper_rdata")
)
expect_named(
list_rdata(
tor_example("mixed"),
Expand Down Expand Up @@ -82,6 +73,35 @@ test_that("list_csv defaults to read from working directory", {
expect_true(any("csv" %in% names(list_csv())))
})

test_that("list_csv is sensitive to `regexp`, `invert, and `ignore.case`", {
expect_named(
list_csv(
tor_example("csv"),
regexp = "[.]CSV$",
ignore.case = TRUE,
),
c("file1", "file2")
)

expect_named(
list_csv(
tor_example("csv"),
regexp = "[.]CSV$",
ignore.case = FALSE,
invert = TRUE
),
c("file1", "file2")
)

expect_error(
list_csv(
tor_example("csv"),
regexp = "[.]CSV$",
ignore.case = FALSE,
)
)
})

context("list_tsv")

test_that("list_tsv lists .tsv files", {
Expand All @@ -95,3 +115,32 @@ test_that("list_tsv lists .tsv files", {
test_that("list_tsv defaults to read from working directory", {
expect_named(list_tsv(), "tsv")
})

test_that("list_tsv is sensitive to `regexp`, `invert, and `ignore.case`", {
expect_named(
list_csv(
tor_example("tsv"),
regexp = "[.]TSV$",
ignore.case = TRUE,
),
c("tsv1", "tsv2")
)

expect_named(
list_csv(
tor_example("tsv"),
regexp = "[.]TSV$",
ignore.case = FALSE,
invert = TRUE
),
c("tsv1", "tsv2")
)

expect_error(
list_csv(
tor_example("tsv"),
regexp = "[.]TSV$",
ignore.case = FALSE,
)
)
})

0 comments on commit e989bd5

Please sign in to comment.