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

https://github.com/jonthegeek/stbl

Stabilize Function Arguments
https://github.com/jonthegeek/stbl

Last synced: 30 days ago
JSON representation

Stabilize Function Arguments

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%"
)
```

# stbl

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/stbl)](https://CRAN.R-project.org/package=stbl)
[![Codecov test coverage](https://codecov.io/gh/jonthegeek/stbl/branch/main/graph/badge.svg)](https://app.codecov.io/gh/jonthegeek/stbl?branch=main)
[![R-CMD-check](https://github.com/jonthegeek/stbl/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/jonthegeek/stbl/actions/workflows/R-CMD-check.yaml)

R is flexible about classes.
Variables are not declared with explicit classes, and arguments of the "wrong" class don't cause errors until they explicitly fail at some point in the call stack.
It would be helpful to keep that flexibility from a user standpoint, but to error informatively and quickly if the inputs will not work for a computation.
The purpose of stbl is to allow programmers to specify what they want, and to then see if what the user supplied can work for that purpose.

## Installation

::: {.pkgdown-release}
Install the released version of stbl from [CRAN](https://cran.r-project.org/):

``` r
install.packages("stbl")
```
:::

::: {.pkgdown-devel}
Install the development version of stbl from [GitHub](https://github.com/):

``` r
# install.packages("pak")
pak::pak("jonthegeek/stbl")
```
:::

## Usage

Use within functions to give meaningful error messages for bad argument classes.

For example, perhaps you would like to protect against the case where data is not properly translated from character on load.

### Without stbl:

Without the argument-stabilizers provided in stbl, error messages can be cryptic, and errors trigger when you might not want them to.

```{r no-stbl, error = TRUE}
my_old_fun <- function(my_arg_name) {
my_arg_name + 1
}
my_old_fun("1")
```

### With stbl:

stbl helps to ensure that arguments are what you expect them to be.

```{r with-stbl-ok}
my_fun <- function(my_arg_name) {
my_arg_name <- stbl::to_int(my_arg_name)
my_arg_name + 1
}
my_fun("1")
```

Failures are reported with helpful messages.

```{r with-stbl-error1, error = TRUE}
my_fun("1.1")
```

The errors help locate issues within vectors.

```{r with-stbl-error2, error = TRUE}
my_fun(c("1", "2", "3.1", "4", "5.2"))
```

## Code of Conduct

Please note that the stbl project is released with a [Contributor Code of Conduct](https://jonthegeek.github.io/stbl/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.