Skip to content

Commit

Permalink
file_path_checking add output default error
Browse files Browse the repository at this point in the history
file_path_checking add output default error, output is "error", "message" or "logical" for #7 second comment
  • Loading branch information
OceaneBouhineauIRD committed Jul 12, 2024
1 parent 6801c86 commit 00e86b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
41 changes: 25 additions & 16 deletions R/file_path_checking.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#' @description Checking for file path before import in R.
#' @param file_path Object of class \code{\link[base]{character}} expected. File location on your system.
#' @param extension Object of class \code{\link[base]{vector}} of {\link[base]{character}} expected. Expected extension type set (avoid the . before the extension).
#' @param output {\link[base]{character}} expected. Kind of expected output. By default "message". You can choose between "message" or "logical".
#' @param output {\link[base]{character}} expected. Kind of expected output. By default "error". You can choose between "error", "message" or "logical".
#' @return The function returns error with output is "error" and if the parameters are not respected, a {\link[base]{character}} with output is "message", a {\link[base]{logical}} with output is "logical"
#' @export
file_path_checking <- function(file_path,
extension,
output = "message") {
output = "error") {
# global process ----
if (r_type_checking(r_object = file_path,
type = "character",
Expand All @@ -28,38 +29,46 @@ file_path_checking <- function(file_path,
if (r_type_checking(r_object = output,
type = "character",
length = 1L,
allowed_value = c("message",
"logical"),
allowed_value = c("error",
"message",
"logical"),
output = "logical") != TRUE) {
return(r_type_checking(r_object = output,
type = "character",
length = 1L,
allowed_value = c("message",
"logical"),
allowed_value = c("error",
"message",
"logical"),
output = "message"))
}
file_path_extension <- dplyr::last(x = unlist(x = strsplit(
x = file_path,
split = "[.]"
)))
if (! file_path_extension %in% extension) {
message_failure <- paste0(format(x = Sys.time(),
"%Y-%m-%d %H:%M:%S"),
" - Failure,",
" invalid \"extension\" argument.\n",
"File extension expected should be ", paste0("\"", extension, collapse = "\", "), "\" (extension provides is \"", file_path_extension, "\").\n")
if (output == "message") {
return(cat(paste0(format(x = Sys.time(),
"%Y-%m-%d %H:%M:%S"),
" - Failure,",
" invalid \"extension\" argument.\n",
"File extension expected should be ", paste0("\"", extension, collapse = "\", "), "\" (extension provides is \"", file_path_extension, "\").\n")))
return(cat(message_failure))
} else if (output == "error") {
return(stop(message_failure))
} else if (output == "logical") {
return(FALSE)
}
}
if (! file.exists(file_path)) {
message_failure <- paste0(format(x = Sys.time(),
"%Y-%m-%d %H:%M:%S"),
" - Failure,",
" invalid \"file_path\" argument.\n",
"No file located at the file path.\n")
if (output == "message") {
return(cat(paste0(format(x = Sys.time(),
"%Y-%m-%d %H:%M:%S"),
" - Failure,",
" invalid \"file_path\" argument.\n",
"No file located at the file path.\n")))
return(cat(message_failure))
} else if (output == "error") {
return(stop(message_failure))
} else if (output == "logical") {
return(FALSE)
}
Expand Down
7 changes: 5 additions & 2 deletions man/file_path_checking.Rd

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

0 comments on commit 00e86b2

Please sign in to comment.