Skip to content

Commit

Permalink
fix error: invalid 'pattern' argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Nov 5, 2023
1 parent dec5700 commit 34f598b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
14 changes: 3 additions & 11 deletions R/fda_drugs.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ fda_drugs_load <- function(file, use = "Products", list = FALSE, dir = faers_cac
if (list) {
list.files(path)
} else {
file <- locate_files(path, use, ignore.case = TRUE)
if (length(file) > 1L) {
cli::cli_abort(
"Multiple files matched, files: {.file {basename(file)}}"
)
}
# Don't use data.table
# Stopped early on line
file <- locate_file(path, use, ignore.case = TRUE)
# Don't use data.table: error, Stopped early on line
out <- vroom::vroom(file, show_col_types = FALSE)
data.table::setDT(out)[]
}
Expand All @@ -47,9 +41,7 @@ fda_drugs_load <- function(file, use = "Products", list = FALSE, dir = faers_cac
fda_drugs_file <- function(dir = faers_cache_dir("fdadrugs")) {
file <- tryCatch(
locate_files(dir, "^fda_drugs_data.*\\.zip", ignore.case = FALSE),
no_file = function(cnd) {
FALSE
}
no_file = function(cnd) FALSE
)
if (isFALSE(file)) {
file <- fda_drugs_download(dir = dir)
Expand Down
21 changes: 20 additions & 1 deletion R/utils-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ unzip2 <- function(path, compress_dir, ignore.case = TRUE) {
}

locate_dir <- function(path, pattern = NULL, ignore.case = TRUE) {
path <- list.dirs(path, recursive = FALSE)
path <- list.dirs(path, full.names = TRUE, recursive = FALSE)
if (!is.null(pattern)) {
path <- path[
str_detect(basename(path), pattern, ignore.case = ignore.case)
]
} else {
pattern <- "any"
}
if (length(path) > 1L) {
cli::cli_abort(
"Multiple directories matched, dir: {.path {basename(path)}}"
)
}
if (!length(path) || !dir.exists(path)) {
cli::cli_abort(
Expand All @@ -103,12 +110,24 @@ locate_dir <- function(path, pattern = NULL, ignore.case = TRUE) {
path
}

locate_file <- function(path, pattern = NULL, ignore.case = TRUE) {
file <- locate_files(path, pattern = pattern, ignore.case = ignore.case)
if (length(file) > 1L) {
cli::cli_abort(
"Multiple files matched, files: {.file {basename(file)}}"
)
}
file
}

locate_files <- function(path, pattern = NULL, ignore.case = TRUE) {
files <- list.files(path, full.names = TRUE)
if (!is.null(pattern)) {
files <- files[
str_detect(basename(files), pattern, ignore.case = ignore.case)
]
} else {
pattern <- "any"
}
if (!length(files)) {
cli::cli_abort(
Expand Down
6 changes: 0 additions & 6 deletions tests/testthat/test-utils.R

This file was deleted.

17 changes: 17 additions & 0 deletions tests/testthat/test_utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
testthat::test_that("utils-file works well", {
dir <- tempdir()
dir.create(file.path(dir, "check_dir1"), recursive = TRUE)
dir.create(file.path(dir, "check_dir2"), recursive = TRUE)
file.create(file.path(dir, "check_file1"))
file.create(file.path(dir, "check_file2"))
testthat::expect_no_error(locate_file(dir, "^check_file1$"))
testthat::expect_no_error(locate_file(dir, "^check_file2$"))
testthat::expect_error(locate_file(dir, "^noneexist_file$"))
testthat::expect_no_error(locate_files(dir))
testthat::expect_no_error(locate_files(dir, "^check_file"))
testthat::expect_error(locate_files(dir, "^noneexist_file$"))
testthat::expect_no_error(locate_dir(dir, "^check_dir1$"))
testthat::expect_no_error(locate_dir(dir, "^check_dir2$"))
testthat::expect_error(locate_dir(dir, "^check_dir"))
testthat::expect_error(locate_dir(dir, "^noneexist_directory$"))
})

0 comments on commit 34f598b

Please sign in to comment.