From 34f598bca20317d3dda6417e75714a4317330534 Mon Sep 17 00:00:00 2001 From: yun Date: Sun, 5 Nov 2023 20:04:47 +0800 Subject: [PATCH] fix error: invalid 'pattern' argument --- R/fda_drugs.R | 14 +++----------- R/utils-file.R | 21 ++++++++++++++++++++- tests/testthat/test-utils.R | 6 ------ tests/testthat/test_utils.R | 17 +++++++++++++++++ 4 files changed, 40 insertions(+), 18 deletions(-) delete mode 100644 tests/testthat/test-utils.R create mode 100644 tests/testthat/test_utils.R diff --git a/R/fda_drugs.R b/R/fda_drugs.R index 3a12630..a6b9e42 100644 --- a/R/fda_drugs.R +++ b/R/fda_drugs.R @@ -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)[] } @@ -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) diff --git a/R/utils-file.R b/R/utils-file.R index df442f4..0ea3091 100644 --- a/R/utils-file.R +++ b/R/utils-file.R @@ -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( @@ -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( diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R deleted file mode 100644 index b34c4df..0000000 --- a/tests/testthat/test-utils.R +++ /dev/null @@ -1,6 +0,0 @@ -testthat::test_that("locate_files() works well", { - testthat::expect_no_error(locate_files(".")) - testthat::expect_error(locate_dir(".", ".txt$")) - testthat::expect_no_error(locate_files(".")) - testthat::expect_error(locate_dir(".", "noneexist_directory")) -}) diff --git a/tests/testthat/test_utils.R b/tests/testthat/test_utils.R new file mode 100644 index 0000000..bca63db --- /dev/null +++ b/tests/testthat/test_utils.R @@ -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$")) +})