Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/flujoo/erify
R Package for Validation of Arguments and Generation of Readable Error Messages
https://github.com/flujoo/erify
error-handling r r-package rstats
Last synced: about 1 month ago
JSON representation
R Package for Validation of Arguments and Generation of Readable Error Messages
- Host: GitHub
- URL: https://github.com/flujoo/erify
- Owner: flujoo
- License: other
- Created: 2021-04-02T06:03:17.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-30T12:33:01.000Z (6 months ago)
- Last Synced: 2024-11-01T06:51:35.590Z (about 1 month ago)
- Topics: error-handling, r, r-package, rstats
- Language: R
- Homepage: https://flujoo.github.io/erify/
- Size: 437 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - flujoo/erify - R Package for Validation of Arguments and Generation of Readable Error Messages (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
error = TRUE
)
```# erify
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
Check arguments and generate readable error messages.
## Motivation
When creating functions for other people to use, you always need to
1. check if the arguments passed by users are valid, and if not,
2. generate informative and well-formatted error messages in a consistent
style.erify serves the exact purpose.
## Installation
Install erify from CRAN:
``` r
install.packages("erify")
```Or install the development version from Github:
``` r
# install devtools if not
# install.packages("devtools")devtools::install_github("flujoo/erify")
```## Example
Suppose you are creating a function which prints a string several times
to emphasize it:```{r}
# print `what` `n` times
emphasize <- function(what, n) {
for (i in 1:n) {
cat(what, "\n")
}
}# example
emphasize("You're beautiful!", 3)
```And suppose a novice user accidentally passes a function to argument `what`,
he/she will get an error message which is not very readable:```{r}
emphasize(c, 3)
```You can improve this by adding erify's `check_type()` into `emphasize()`:
```{r}
emphasize <- function(what, n) {
# check the type of `what`
erify::check_type(what, "character")
# main
for (i in 1:n) {
cat(what, "\n")
}
}emphasize(c, 3)
```In the above code, `check_type(what, "character")` checks if `what` has type
character, and if not, generates improved error message.## More
You can add more functions to check arguments, customize error messages,
and create your own check functions.See `vignette("erify")` for a gentle introduction to erify.