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 5f2ee46 commit dec5700
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ parse_ascii <- function(path, year, quarter) {
read_ascii_deleted_cases <- function(path, year, quarter) {
# As of 2019 Quarter one there is a new text file that lists deleted files
if (!is_before_period(year, quarter, 2018L, "q4")) {
deleted_cases_files <- locate_files(locate_dir(path, "^deleted$"), NULL)
deleted_cases_files <- locate_files(locate_dir(path, "^deleted$"))
deleted_cases <- lapply(deleted_cases_files, function(file) {
data.table::fread(
file = file,
Expand Down
22 changes: 14 additions & 8 deletions R/utils-file.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Removes caches
#' Remove caches
#'
#' @param caches An atomic character, indicates what caches to remove? Only
#' `"metadata"`, `"fdadrugs"`, `"rxnorm"`, and `"athena"` can be used. If
Expand All @@ -7,7 +7,7 @@
#' @return Path of the deleted directory invisiblely
#' @examples
#' faers_clearcache()
#' @export
#' @export
faers_clearcache <- function(caches = NULL, force = FALSE) {
if (is.null(caches)) {
paths <- faers_user_cache_dir(create = FALSE)
Expand Down Expand Up @@ -87,9 +87,13 @@ unzip2 <- function(path, compress_dir, ignore.case = TRUE) {
compress_dir
}

locate_dir <- function(path, pattern, ignore.case = TRUE) {
locate_dir <- function(path, pattern = NULL, ignore.case = TRUE) {
path <- list.dirs(path, recursive = FALSE)
path <- path[str_detect(basename(path), pattern, ignore.case = ignore.case)]
if (!is.null(pattern)) {
path <- path[
str_detect(basename(path), pattern, ignore.case = ignore.case)
]
}
if (!length(path) || !dir.exists(path)) {
cli::cli_abort(
"Cannot locate {.field {pattern}} directory in {.path {path}}",
Expand All @@ -99,11 +103,13 @@ locate_dir <- function(path, pattern, ignore.case = TRUE) {
path
}

locate_files <- function(path, pattern, ignore.case = TRUE) {
locate_files <- function(path, pattern = NULL, ignore.case = TRUE) {
files <- list.files(path, full.names = TRUE)
files <- files[
str_detect(basename(files), pattern, ignore.case = ignore.case)
]
if (!is.null(pattern)) {
files <- files[
str_detect(basename(files), pattern, ignore.case = ignore.case)
]
}
if (!length(files)) {
cli::cli_abort(
"Cannot locate {.field {pattern}} file in {.path {path}}",
Expand Down
4 changes: 2 additions & 2 deletions man/faers_clearcache.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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"))
})

0 comments on commit dec5700

Please sign in to comment.