]> nmode's Git Repositories - Rnaught/blob - R/util.R
Add input validation to estimators
[Rnaught] / R / util.R
1 #' Case Counts Validation
2 #'
3 #' This is an internal function called by the estimators. It validates the
4 #' supplied case counts by ensuring it is a vector of integers of length at
5 #' least `min_length` with entries greater than or equal to `min_count`.
6 #' Execution is halted if these requirements are not satisfied.
7 #'
8 #' @param cases The case counts to be validated.
9 #' @param min_length The minimum length of the vector of case counts.
10 #' @param min_count The minimum value of the case count vector's entries.
11 #'
12 #' @noRd
13 validate_cases <- function(cases, min_length, min_count) {
14 if (!is.vector(cases) || !is.numeric(cases) || any(floor(cases) != cases)) {
15 stop("Case counts must be a vector of integers.", call. = FALSE)
16 }
17 if (length(cases) < min_length) {
18 stop(paste("Case counts must have at least", min_length, "entries."),
19 call. = FALSE
20 )
21 }
22 if (any(cases < min_count)) {
23 stop(paste0("Case counts cannot be less than ", min_count, "."),
24 call. = FALSE
25 )
26 }
27 }
28
29 #' Real Number Testing
30 #'
31 #' This is an internal function which checks whether the given argument is a
32 #' real number.
33 #'
34 #' @param x The argument to test for being a real number.
35 #'
36 #' @return `TRUE` if the argument is a real number, `FALSE` otherwise.
37 #'
38 #' @noRd
39 is_real <- function(x) {
40 is.vector(x) && is.numeric(x) && length(x) == 1
41 }
42
43 #' Integer Testing
44 #'
45 #' This is an internal function which checks whether the given argument is an
46 #' integer.
47 #'
48 #' @param n The argument to test for being an integer.
49 #'
50 #' @return `TRUE` if the argument is an integer, `FALSE` otherwise.
51 #'
52 #' @noRd
53 is_integer <- function(n) {
54 is_real(n) && floor(n) == n
55 }