-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.R
More file actions
56 lines (55 loc) · 1.63 KB
/
Copy pathassert.R
File metadata and controls
56 lines (55 loc) · 1.63 KB
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
56
#' Assert Condition
#'
#' @name assert
#'
#' Checks a condition and aborts with an error message if FALSE.
#'
#' @param cond Logical scalar to test.
#' @param ... Error message arguments passed to abort().
#'
#' @return Invisible NULL if condition is TRUE; otherwise stops.
#'
#' @family error-handling
#' @export
#'
#' @examples
#' # Basic assertion that passes silently
#' iaw$assert(1 + 1 == 2, "math is broken")
#'
#' # Glue-style interpolation in the message
#' n <- 5
#' iaw$assert(n > 0, paste("n must be positive, got", n))
#'
#' # Common pattern: validate function arguments
#' \dontrun{
#' check_input <- function(x) {
#' iaw$assert(is.numeric(x), paste("x must be numeric, got", class(x)))
#' iaw$assert(length(x) > 0, "x must not be empty")
#' }
#' check_input(c(1, 2, 3)) # passes
#' check_input("oops") # aborts with message
#' }
#'
#' # Validate portfolio weights sum to 1
#' weights <- c(0.4, 0.35, 0.25)
#' iaw$assert(abs(sum(weights) - 1) < 1e-10, "weights must sum to 1") # passes
#'
#' # Assert catches the failure and returns a clean error via tryCatch
#' err <- tryCatch(
#' iaw$assert(FALSE, "expected positive returns"),
#' error = function(e) e$message
#' )
#' err # "expected positive returns"
#'
#' # Guard data dimensions before matrix operations
#' m <- matrix(1:12, nrow = 3)
#' iaw$assert(nrow(m) >= 2, paste("need >= 2 rows, got", nrow(m))) # passes
iaw$assert <- function(cond, ...) {
stopifnot(is.logical(cond), length(cond) == 1L)
if (!cond) {
msg <- paste(...)
if (nchar(msg) == 0) msg <- paste(deparse(substitute(cond)), collapse = " ")
iaw$abort(msg)
}
invisible(NULL)
}