Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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)

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 <- TRUE

dclr_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)
```