-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function input checks #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue as average wait regarding the use of the default value into check_lengths_match |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue as average wait regarding the use of the default value into check_lengths_match |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#' Check Class of Argument Inputs | ||
#' @param ... Objects to be checked for class. | ||
#' @param .expected_class Character. The name of the class against which objects | ||
#' will be checked. | ||
#' @noRd | ||
check_class <- function(..., .expected_class = "numeric", .call = rlang::caller_env()) { | ||
|
||
args <- rlang::dots_list(..., .named = TRUE) | ||
|
||
args_class_lgl <- lapply( | ||
args, | ||
function(arg) inherits(arg, .expected_class) | ||
) | ||
|
||
failing_arg_names <- names(Filter(isFALSE, args_class_lgl)) | ||
|
||
if (length(failing_arg_names) > 0) { | ||
|
||
failing_args <- args[names(args) %in% failing_arg_names] | ||
failing_args_classes <- sapply(failing_args, class) | ||
|
||
cli::cli_abort( | ||
message = c( | ||
"{.var {failing_arg_names}} must be of class {.cls {(.expected_class)}}", | ||
x = "You provided input of class {.cls {failing_args_classes}}." | ||
), | ||
call = .call | ||
) | ||
} | ||
|
||
} | ||
|
||
#' Check that Lengths of Argument Inputs Match | ||
#' @param ... Objects to be compared for length. | ||
#' @noRd | ||
check_lengths_match <- function(..., .call = rlang::caller_env()) { | ||
|
||
args <- rlang::dots_list(..., .named = TRUE) | ||
|
||
arg_lengths <- lengths(args) | ||
|
||
lengths_are_equal <- length(unique(arg_lengths)) == 1 | ||
|
||
if (!lengths_are_equal) { | ||
cli::cli_abort( | ||
message = c( | ||
"{.arg {names(args)}} must be the same length.", | ||
x = "You provided inputs with lengths of {paste(arg_lengths)}." | ||
), | ||
call = .call | ||
) | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a case here where specifying a vector of length >1 into target_wait, and relying on default factor =4. Would this defeat
check_lengths_match()
? Might need a check on the use of default and extend to same length vector.