Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davisvaughan/declair
What the Package Does (One Line, Title Case)
https://github.com/davisvaughan/declair
Last synced: 7 days ago
JSON representation
What the Package Does (One Line, Title Case)
- Host: GitHub
- URL: https://github.com/davisvaughan/declair
- Owner: DavisVaughan
- Created: 2019-03-22T21:11:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-03-22T21:20:48.000Z (over 5 years ago)
- Last Synced: 2024-10-09T22:10:18.589Z (28 days ago)
- Language: R
- Size: 6.84 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)options(rlang_backtrace_on_error = "none")
```# declair
The goal of declair is to provide standardized validation functions. It is inspired by `vctrs::vec_assert()` and motivated by frustration and duplication across packages when validating user input.
## Installation
You can install the released version of declair from [CRAN](https://CRAN.R-project.org) with:
``` r
# no you cannot
install.packages("declair")
``````r
devtools::install_github("DavisVaughan/declair")
```## Example
```{r}
library(declair)
```Nice error messages based on type
```{r, error=TRUE}
dclr_is_character(1)
```Expressions are auto labeled
```{r, error=TRUE}
x <- 1
dclr_is_character(x)
```Works with function arguments
```{r, error=TRUE}
fn <- function(my_arg) {
dclr_is_scalar_character(my_arg)
}fn(x)
fn(c("x", "y"))
```Lots of extra helpers based on rlang goodies
```{r, error=TRUE}
dclr_has_name(mtcars, "x")
``````{r, error=TRUE}
dclr_is_integerish(1.5)dclr_is_integerish(1)
dclr_is_bool(1)
```Combine multiple checks with `dclr_or()` and `dclr_and()`
```{r, error = TRUE}
x <- 1L# All good because the second condition passes
dclr_or(dclr_is_character(x), dclr_is_integer(x))# This fails and tells you which condition caused the issue
dclr_and(dclr_is_character(x), dclr_is_integer(x))# This fails because neither pass
dclr_or(dclr_is_character(x), dclr_has_name(x, "a name"))
````dclr_or()` is particularly useful if an argument defaults to `NULL` but can be other things.
```{r, error = TRUE}
x <- NULL# All good
dclr_or(dclr_is_character(x), dclr_is_null(x))x <- "hi"
# All good
dclr_or(dclr_is_character(x), dclr_is_null(x))# Not good!
x <- TRUEdclr_or(dclr_is_character(x), dclr_is_null(x))
```You can use `dclr()` on any custom ptype and size.
```{r, error=TRUE}
dclr(1, ptype = glue::glue(), size = 1)
```