aboutsummaryrefslogtreecommitdiff
path: root/R/util.R
blob: d8b0b5913bf7cb4507612071522bf0ec5d25b283 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#' Case Counts Validation
#'
#' This is an internal function called by the estimators. It validates the
#' supplied case counts by ensuring it is a vector of integers of length at
#' least `min_length` with entries greater than or equal to `min_count`.
#' Execution is halted if these requirements are not satisfied.
#'
#' @param cases The case counts to be validated.
#' @param min_length The minimum length of the vector of case counts.
#' @param min_count The minimum value of the case count vector's entries.
#'
#' @noRd
validate_cases <- function(cases, min_length, min_count) {
  if (!is.vector(cases) || !is.numeric(cases) || any(floor(cases) != cases)) {
    stop("Case counts must be a vector of integers.", call. = FALSE)
  }
  if (length(cases) < min_length) {
    stop(paste("Case counts must have at least", min_length, "entries."),
      call. = FALSE
    )
  }
  if (any(cases < min_count)) {
    stop(paste0("Case counts cannot be less than ", min_count, "."),
      call. = FALSE
    )
  }
}

#' Real Number Testing
#'
#' This is an internal function which checks whether the given argument is a
#' real number.
#'
#' @param x The argument to test for being a real number.
#'
#' @return `TRUE` if the argument is a real number, `FALSE` otherwise.
#'
#' @noRd
is_real <- function(x) {
  is.vector(x) && is.numeric(x) && length(x) == 1
}

#' Integer Testing
#'
#' This is an internal function which checks whether the given argument is an
#' integer.
#'
#' @param n The argument to test for being an integer.
#'
#' @return `TRUE` if the argument is an integer, `FALSE` otherwise.
#'
#' @noRd
is_integer <- function(n) {
  is_real(n) && floor(n) == n
}