Title: Validate Function Arguments
Version: 1.0.1
Description: Lightweight validation tool for checking function arguments and validating data analysis scripts. This is an alternative to stopifnot() from the 'base' package and to assert_that() from the 'assertthat' package. It provides more informative error messages and facilitates debugging.
URL: https://github.com/OlivierBinette/assert
BugReports: https://github.com/OlivierBinette/assert/issues
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
Encoding: UTF-8
LazyData: true
ByteCompile: true
RoxygenNote: 7.1.1
NeedsCompilation: no
Packaged: 2020-11-28 16:33:56 UTC; olivierbinette
Author: Olivier Binette [aut, cre, cph]
Maintainer: Olivier Binette <olivier.binette@gmail.com>
Repository: CRAN
Date/Publication: 2020-11-28 17:50:02 UTC

Assertions and Argument Validation

Description

Assert that each of the provided expression is true. Otherwise stop execution and return a description of each failed assertion.

Usage

assert(..., msg = "", stop = TRUE)

Arguments

...

List of logical assertions.

msg

Message to print alongside the list of failed assertions (if any).

stop

Whether execution should be stopped on failed assertions. If set to FALSE, then only a warning is issued. Default is TRUE.

Value

Nothing if all assertions pass. Otherwise throws an error describing which

Examples


attach(ChickWeight)

# Passing assertions
assert(is.numeric(weight),
       all(weight > 0))

# Failing assertions
if (interactive()) {
  assert(all(Diet > 0),
         is.numeric(Times))
}

# Validating function arguments
sum <- function(a, b) {
  assert(is.numeric(a),
         is.numeric(b),
         length(a) == length(b))

  return(a+b)
}

sum(2, 2)

if (interactive()) {
  sum("1", 2)
  sum(1, c(1,2))
  sum(1, x)
}