loading...

stop_equal() generates an error message for == assertions.

stop_equal(
  lhs,
  rhs,
  ...,
  mod = NULL,
  na.rm = FALSE,
  test_it = TRUE,
  par. = list()
)

`stop_==`(
  lhs,
  rhs,
  ...,
  mod = NULL,
  na.rm = FALSE,
  test_it = TRUE,
  par. = list()
)

Arguments

lhs

The left-hand side of the comparison.

rhs

The right-hand side of the comparison.

...

Additional arguments (not used yet).

mod

A character string indicating the modifier to use for the comparison ("", "any", or "all"). Default is NULL, which is equivalent to "".

na.rm

Logical, indicating whether missing values should be removed before performing the comparison. Default is FALSE.

test_it

Logical, indicating whether the comparison should be actually tested. Default is TRUE. If FALSE, the function only constructs the error message based on the lengths of lhs and rhs, and the presence of missing values.

par.

A list of parameters to customize the error message. Optional fields include msg (message(s) to display at the top), footer(message(s) to add at the bottom), arg (name of first argument), arg2 (name of second argument), id (error class id), mod (the modifier, see mod above), call (the call where the error was generated).

Value

This function always stops with an error.

Examples

stop_equal(1, 2) |> try() # A little bit silly with two constants
#> Error in eval(expr, envir) : ! `1` must be == `2`.
#>  It is silly to compare two constants, may be check you code.
x <- 1
stop_equal(x, 2) |> try()
#> Error in eval(expr, envir) : ! `x` must be == `2`.
#>  `x` is: 1.
x <- 1:2
stop_equal(x, 1) |> try() # Not length 1
#> Error in eval(expr, envir) : 
#>   ! Both `x` and `1` must have length 1.
#>  Length of the first one: 2.
stop_equal(x, 1:2, mod = "all") |> try() # Should not call stop_equal() here
#> Error in eval(expr, envir) : 
#>   ! All are TRUE in `x` == `1:2`.
#>  Should be OK, but `stop_equal()` called anyway.
#>  This is an internal error, please report it to the package authors.
stop_equal(x, 1:3, mod = "all") |> try()
#> Error in eval(expr, envir) : ! Not all `x` are == `1:3`.
#>  Must all be ==.
stop_equal(x, 1:3, mod = "any") |> try() # Should not call stop_equal() here
#> Error in eval(expr, envir) : 
#>   ! At least one is TRUE in `x` == `1:3`.
#>  Should be OK, but `stop_equal()` called anyway.
#>  This is an internal error, please report it to the package authors.
stop_equal(x, 2:4, mod = "any")
#> Error: ! Not any `x` is == `2:4`.
#>  Must have at least one ==.
y <- 2
stop_equal(x, y, mod = "all") |> try()
#> Error in eval(expr, envir) : ! Not all `x` are == `y`.
#>  Must all be ==.
stop_equal(x, y, mod = "any") |> try() # Should not call stop_equal() here
#> Error in eval(expr, envir) : 
#>   ! At least one is TRUE in `x` == `y`.
#>  Should be OK, but `stop_equal()` called anyway.
#>  This is an internal error, please report it to the package authors.
z <- c(NA, 5, NA)
stop_equal(x, z, mod = "all") |> try()
#> Error in eval(expr, envir) : 
#>   ! 2 missing values when comparing `x` with `z`.
#>  May not have missing values.
stop_equal(x, z, mod = "all", na.rm = TRUE) |> try()
#> Error in eval(expr, envir) : ! Not all `x` are == `z`.
#>  Must all be ==.
stop_equal(x, z, mod = "any", na.rm = TRUE) |> try()
#> Error in eval(expr, envir) : ! Not any `x` is == `z`.
#>  Must have at least one ==.
stop_equal(length(x), 1L) |> try()
#> Error in eval(expr, envir) : 
#>   ! `length(x)` must be == `1L`.
#>  `length(x)` is: 2.
stop_equal(NULL, 2L) |> try()
#> Error in eval(expr, envir) : ! `NULL` has length 0.
#>  Must have length 1.
#>  Did you mean `is.null(2L)` instead of `NULL == 2L`?
# Extra messages
stop_equal(y, 2, par. = list(msg = c("Some info...", x = "This is wrong"),
  footer = c("*" = "A footer..."))) |> try()
#> Error in eval(expr, envir) : Some info...
#>  This is wrong
#> Some info...
#>  This is wrong
#> ! `y` == `2`.
#>  Should be OK, but `stop_equal()` called anyway.
#>  A footer...
#>  A footer...
#>  This is an internal error, please report it to the package authors.