Skip to content

Commit 17bc611

Browse files
authored
fix: find_file() and messages (#131)
1 parent 98f45da commit 17bc611

File tree

7 files changed

+180
-131
lines changed

7 files changed

+180
-131
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: connector
22
Title: Streamlining Data Access in Clinical Research
3-
Version: 1.0.0
3+
Version: 1.0.0.9000
44
Authors@R: c(
55
person("Cervan", "Girard", , "cgid@novonordisk.com", role = c("aut", "cre")),
66
person("Aksel", "Thomsen", , "oath@novonordisk.com", role = "aut"),

R/utils_files.R

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
#' Find File
22
#'
3+
#' Finds files in the following order:
4+
#'
5+
#' 1. The file is fully specified and exists (no message)
6+
#' 2. Only one file is found with that name (with message)
7+
#' 3. A file with the default extension exists (with message)
8+
#'
39
#' @param name Name of a file
410
#' @param root Path to the root folder
5-
#'
611
#' @return A full name path to the file or a error if multiples files or 0.
712
#' @noRd
813
find_file <- function(name, root) {
14+
file <- file.path(root, name)
15+
16+
if (file.exists(file)) {
17+
return(file)
18+
}
19+
920
files <- list.files(
1021
path = root,
11-
pattern = paste0("^", name, "(\\.|$)"),
22+
pattern = paste0("^", name, "(\\.[[:alnum:]]+|)$"),
1223
full.names = TRUE
1324
)
1425

26+
if (!length(files)) {
27+
cli::cli_abort(
28+
"No file found with name: {.field {name}}"
29+
)
30+
}
31+
1532
if (length(files) == 1) {
1633
zephyr::msg(
1734
"Found one file: {.file {files}}"
@@ -20,13 +37,13 @@ find_file <- function(name, root) {
2037
}
2138

2239
ext <- zephyr::get_option("default_ext", "connector")
23-
files <- files[tools::file_ext(files) == ext]
40+
file_ext <- files[tools::file_ext(files) == ext]
2441

25-
if (length(files) == 1) {
42+
if (length(file_ext) == 1) {
2643
zephyr::msg(
27-
"Found one file with default ({.field {ext}}) extension: {.file {files}}"
44+
"Found one file with default ({.field {ext}}) extension: {.file {file_ext}}"
2845
)
29-
return(files)
46+
return(file_ext)
3047
}
3148

3249
cli::cli_abort(

0 commit comments

Comments
 (0)