Check number of columns
check_ncol.RdCheck if arg is a 2-dimensional object with number of columns equal to or greater/less than or equal to ncol.
Usage
check_ncol_equals(arg, ncol, signal = "error", msg = NULL, call. = FALSE)
check_ncol_leq(arg, ncol, signal = "error", msg = NULL, call. = FALSE)
check_ncol_geq(arg, ncol, signal = "error", msg = NULL, call. = FALSE)Arguments
- arg
(object)
The argument to check.- ncol
(scalar integer)
The number of columns to check.- signal
(string:
c("error", "warning", "message"))
Must be one of"error","warning", or"message"."error"(default) callsbase::stop()to signal an error message."warning"callsbase::warning()to signal a warning message."message"callsbase::message()to signal a message.- msg
(string or
NULL:NULL)
A string that replaces the default message. Set asNULLto use the default message.- call.
(Scalar logical:
c(FALSE, TRUE))
Passed tostop()orwarning(). The defaultmsgincludes it's own formatted call, hencecall.should beFALSE(default). If you insert your ownmsg, then you may wantcall. = TRUE.
See also
Other check_dims:
check_nrow_equals()
Examples
#----------------------------------------------------------------------------
# check_ncol*() examples
#----------------------------------------------------------------------------
library(bkcheck)
#----------------------------------------------------------------------------
# check_ncol_equals()
#----------------------------------------------------------------------------
f <- function(x) {
x |>
check_ncol_equals(1L)
}
f(data.frame(1))
try(f(data.frame(1:2, 1:2)))
#> Error : Argument `x` must have 1 column.
#>
#> ncol(x) = 2
#>
#> Call: f(x = data.frame(1:2, 1:2))
#----------------------------------------------------------------------------
# check_ncol_leq()
#----------------------------------------------------------------------------
f <- function(x) {
x |>
check_ncol_leq(2L)
}
f(matrix(1))
try(f(data.frame(1, 2, 3)))
#> Error : Argument `x` must have at most 2 columns.
#>
#> ncol(x) = 3
#>
#> Call: f(x = data.frame(1, 2, 3))
#----------------------------------------------------------------------------
# check_ncol_geq()
#----------------------------------------------------------------------------
f <- function(x) {
x |>
check_data_frame() |>
check_ncol_geq(1L)
}
f(data.frame(1))
try(f(data.frame()))
#> Error : Argument `x` must have at least 1 column.
#>
#> ncol(x) = 0
#>
#> Call: f(x = data.frame())