• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

R/H21-Mar-2019-640282

man/H07-Apr-2017-426374

tests/H21-Mar-2019-323258

DESCRIPTIONH A D21-Mar-2019884 2423

MD5H A D21-Mar-20191.8 KiB3534

NAMESPACEH A D07-Apr-2017522 2927

README.mdH A D25-Jun-20183.6 KiB9567

README.md

1# assertthat
2
3[![Travis-CI Build Status](https://travis-ci.org/hadley/assertthat.svg?branch=master)](https://travis-ci.org/hadley/assertthat)
4[![Coverage status](https://codecov.io/gh/hadley/assertthat/branch/master/graph/badge.svg)](https://codecov.io/github/hadley/assertthat?branch=master)
5
6assertthat provides a drop in replacement for `stopifnot()` that makes it easy to check the pre- and post-conditions of a function, while producing useful error messages.
7
8```R
9x <- 1:10
10stopifnot(is.character(x))
11# Error: is.character(x) is not TRUE
12
13assert_that(is.character(x))
14# Error: x is not a character vector
15
16assert_that(length(x) == 5)
17# Error: length(x) not equal to 5
18
19assert_that(is.numeric(x))
20# [1] TRUE
21```
22
23This is a good defensive programming technique, and is useful as source-code documentation: you can see exactly what your function expects when you come back to it in the future.  It is partly a response to the lack of static typing in R, but it allows you to test for general conditions (like `length(x) == length(y)`) that are difficult to express in a type system.
24
25`assertthat` can be installed either from CRAN:
26
27```R
28install.packages('assertthat')
29```
30
31or with devtools:
32
33```R
34devtools::install_github("hadley/assertthat")
35```
36
37## New assertions
38
39As well as all the functions provided by R, assertthat provides a few more that I use a lot:
40
41* `is.flag(x)`: is x `TRUE` or `FALSE`? (a boolean flag)
42* `is.string(x)`: is x a length 1 character vector?
43* `has_name(x, nm)`, `x %has_name% nm`: does `x` have component `nm`?
44* `has_attr(x, attr)`, `x %has_attr% attr`: does `x` have attribute `attr`?
45* `is.count(x)`: is x a single positive integer?
46* `are_equal(x, y)`: are `x` and `y` equal?
47* `not_empty(x)`: are all dimensions of `x` greater than 0?
48* `noNA(x)`: is `x` free from missing values?
49* `is.dir(path)`: is `path` a directory?
50* `is.writeable(path)`/`is.readable(path)`: is `path` writeable/readable?
51* `has_extension(path, extension)`: does `file` have given `extension`?
52
53## `assert_that`, `see_if` and `validate_that`
54
55There are three main functions in assertthat:
56
57* `assert_that()` signal an error
58
59* `see_if()` returns a logical value, with the error message as an attribute.
60
61* `validate_that()` returns `TRUE` on success, otherwise returns the error as
62  a string.
63
64You'll use `assert_that()` in your own code, but you'll mostly see `see_if()` in the examples (because `R CMD check` requires that examples run without errors). Use `validate_that()` for S4 validate methods.
65
66## Writing your own assertions
67
68If you're writing your own assertions, you can provide custom error messages using the `on_failure()` helper:
69
70```R
71is_odd <- function(x) {
72  assert_that(is.numeric(x), length(x) == 1)
73  x %% 2 == 1
74}
75assert_that(is_odd(2))
76# Error: is_odd(x = 2) is not TRUE
77
78on_failure(is_odd) <- function(call, env) {
79  paste0(deparse(call$x), " is even")
80}
81assert_that(is_odd(2))
82# Error: 2 is even
83```
84
85The `on_failure` callback is called with two arguments, the unevaluated function `call`  (which has already been standardised with `match.call()`), and `env`, and the environment in which the assertion was executed. This allows you to choose between displaying values or names in your error messages. Read the [advanced R book](http://adv-r.had.co.nz/Expressions.html) to learn more about working with calls.
86
87Also note the use of `assert_that()` in our new function: assertions flow through function calls ensuring that you get a useful error message at the top level:
88
89```R
90assert_that(is_odd("b"))
91# Error: x is not a numeric or integer vector
92assert_that(is_odd(1:2))
93# Error: length(x) not equal to 1
94```
95