Skip to contents

Check if arg is a 2-dimensional object with number of rows equal to or greater/less than or equal to nrow.

Usage

check_nrow_equals(arg, nrow, signal = "error", msg = NULL, call. = FALSE)

check_nrow_leq(arg, nrow, signal = "error", msg = NULL, call. = FALSE)

check_nrow_geq(arg, nrow, signal = "error", msg = NULL, call. = FALSE)

Arguments

arg

(object)
The argument to check.

nrow

(scalar integer)
The number of rows to check.

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.

See also

Other check_dims: check_ncol_equals()

Examples

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

#----------------------------------------------------------------------------
# check_nrow_equals()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_nrow_equals(1L)
}
f(data.frame(1))
try(f(data.frame(1:2)))
#> Error : Argument `x` must have 1 row.
#> 
#> nrow(x) = 2
#> 
#> Call: f(x = data.frame(1:2))

#----------------------------------------------------------------------------
# check_nrow_leq()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_nrow_leq(2L)
}
f(matrix(1))
try(f(data.frame(1:3, 2:4, 3:5)))
#> Error : Argument `x` must have at most 2 rows.
#> 
#> nrow(x) = 3
#> 
#> Call: f(x = data.frame(1:3, 2:4, 3:5))

#----------------------------------------------------------------------------
# check_nrow_geq()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_data_frame() |>
    check_nrow_geq(1L)
}
f(data.frame(1))
try(f(data.frame()))
#> Error : Argument `x` must have at least 1 row.
#> 
#> nrow(x) = 0
#> 
#> Call: f(x = data.frame())