Skip to contents

Check if arg is a monotonic (increasing or decreasing) numeric vector.

Usage

check_monotonic_decreasing(
  arg,
  na.rm = FALSE,
  signal = "error",
  msg = NULL,
  call. = FALSE
)

check_monotonic_increasing(
  arg,
  na.rm = FALSE,
  signal = "error",
  msg = NULL,
  call. = FALSE
)

Arguments

arg

(object)
The argument to check.

na.rm

(scalar logical: c(FALSE, TRUE))
Whether or not to remove missing values before checking.

signal

(string: c("error", "warning", "message"))
Must be one of "error", "warning", or "message". "error" (default) calls base::stop() to signal an error message. "warning" calls base::warning() to signal a warning message. "message" calls base::message() to signal a message.

msg

(string or NULL: NULL)
A string that replaces the default message. Set as NULL to use the default message.

call.

(Scalar logical: c(FALSE, TRUE))
Passed to stop() or warning(). The default msg includes it's own formatted call, hence call. should be FALSE (default). If you insert your own msg, then you may want call. = TRUE.

Value

invisible(arg) or condition determined by argument signal.

Examples

#----------------------------------------------------------------------------
# check_monotonic_*() examples
#----------------------------------------------------------------------------
library(bkcheck)

#----------------------------------------------------------------------------
# check_monotonic_decreasing()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_numeric() |>
    check_monotonic_decreasing()
}

f(10:1)
f(c(3, 3, 2, 1))
try(f(1:2))
#> Error : Argument `x` must be monotonically decreasing.
#> 
#> First violation at index 2: x[1] = 1, x[2] = 2
#> class(x) = 'integer'
#> length(x) = 2
#> 
#> Call: f(x = 1:2)

#----------------------------------------------------------------------------
# check_monotonic_increasing()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_numeric() |>
    check_monotonic_increasing()
}

f(1:10)
f(c(1, 1, 2, 2, 3))
try(f(2:1))
#> Error : Argument `x` must be monotonically increasing.
#> 
#> First violation at index 2: x[1] = 2, x[2] = 1
#> class(x) = 'integer'
#> length(x) = 2
#> 
#> Call: f(x = 2:1)