{"id":13858310,"url":"https://github.com/hadley/assertthat","last_synced_at":"2025-04-09T19:19:57.188Z","repository":{"id":1029984,"uuid":"9324319","full_name":"hadley/assertthat","owner":"hadley","description":"User friendly assertions for R","archived":false,"fork":false,"pushed_at":"2023-04-21T14:55:10.000Z","size":124,"stargazers_count":208,"open_issues_count":34,"forks_count":29,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-09T19:19:53.848Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hadley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2013-04-09T15:17:53.000Z","updated_at":"2025-03-27T20:31:28.000Z","dependencies_parsed_at":"2022-08-16T11:50:32.667Z","dependency_job_id":"97b99cbe-ef54-43fc-919f-ca36c5384686","html_url":"https://github.com/hadley/assertthat","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadley%2Fassertthat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadley%2Fassertthat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadley%2Fassertthat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadley%2Fassertthat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hadley","download_url":"https://codeload.github.com/hadley/assertthat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248094991,"owners_count":21046770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-05T03:02:03.747Z","updated_at":"2025-04-09T19:19:57.158Z","avatar_url":"https://github.com/hadley.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"# assertthat\n\n[![Travis-CI Build Status](https://travis-ci.org/hadley/assertthat.svg?branch=master)](https://travis-ci.org/hadley/assertthat)\n[![Coverage status](https://codecov.io/gh/hadley/assertthat/branch/master/graph/badge.svg)](https://codecov.io/github/hadley/assertthat?branch=master)\n\nassertthat 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.  \n\n```R\nx \u003c- 1:10\nstopifnot(is.character(x))\n# Error: is.character(x) is not TRUE\n\nassert_that(is.character(x))\n# Error: x is not a character vector\n\nassert_that(length(x) == 5)\n# Error: length(x) not equal to 5\n\nassert_that(is.numeric(x))\n# [1] TRUE\n```\n\nThis 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.\n\n`assertthat` can be installed either from CRAN: \n\n```R\ninstall.packages('assertthat')\n```\n\nor with devtools:\n\n```R\ndevtools::install_github(\"hadley/assertthat\")\n```\n\n## New assertions\n\nAs well as all the functions provided by R, assertthat provides a few more that I use a lot:\n\n* `is.flag(x)`: is x `TRUE` or `FALSE`? (a boolean flag)\n* `is.string(x)`: is x a length 1 character vector?\n* `has_name(x, nm)`, `x %has_name% nm`: does `x` have component `nm`?\n* `has_attr(x, attr)`, `x %has_attr% attr`: does `x` have attribute `attr`?\n* `is.count(x)`: is x a single positive integer?\n* `are_equal(x, y)`: are `x` and `y` equal?\n* `not_empty(x)`: are all dimensions of `x` greater than 0?\n* `noNA(x)`: is `x` free from missing values?\n* `is.dir(path)`: is `path` a directory?\n* `is.writeable(path)`/`is.readable(path)`: is `path` writeable/readable?\n* `has_extension(path, extension)`: does `file` have given `extension`?\n\n## `assert_that`, `see_if` and `validate_that`\n\nThere are three main functions in assertthat: \n\n* `assert_that()` signal an error\n\n* `see_if()` returns a logical value, with the error message as an attribute.\n\n* `validate_that()` returns `TRUE` on success, otherwise returns the error as\n  a string.\n\nYou'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.\n\n## Writing your own assertions\n\nIf you're writing your own assertions, you can provide custom error messages using the `on_failure()` helper:\n\n```R\nis_odd \u003c- function(x) {\n  assert_that(is.numeric(x), length(x) == 1)\n  x %% 2 == 1\n}\nassert_that(is_odd(2))\n# Error: is_odd(x = 2) is not TRUE\n\non_failure(is_odd) \u003c- function(call, env) {\n  paste0(deparse(call$x), \" is even\")\n}\nassert_that(is_odd(2))\n# Error: 2 is even\n```\n\nThe `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.\n\nAlso 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:\n\n```R\nassert_that(is_odd(\"b\"))\n# Error: x is not a numeric or integer vector\nassert_that(is_odd(1:2))\n# Error: length(x) not equal to 1\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhadley%2Fassertthat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhadley%2Fassertthat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhadley%2Fassertthat/lists"}