The goal of disputeR is to easily implement unit tests within functions and Shiny modules. Unlike other packages that provide frameworks for developing these tests, disputeR takes a different approach, offering pre-built unit tests that can easily be dropped into functions and test for basic requirements, like class, length, and common values used in function writing (such as NULL). These tests return informative error messages that follow the tidyverse style guide’s recommendations.
Basic Usage
The following example creates a simple function for squaring a numeric value, which is passed to the function using the parameter x. Two disputeR functions are added before squaring x. The first, dis_not_missing(), evaluates x to ensure the parameter is not skipped by the user. The second, dis_numeric(), applies a number of checks to x.
## load package
library(disputeR)
## define example function
example <- function(x){
## check inputs with disputeR
dis_not_missing(.f = missing(x))
dis_numeric(x, null_valid = FALSE)
## square
out <- x^2
## return output
return(out)
}The checks dis_numeric() applies include default settings that require a scalar input (i.e. length(x) == 1) and do not allow NA, NaN, Inf, or -Inf. Each of these can be enabled using the parameters for dis_numeric() at the function author’s discretion. In addition, we enforce a requirement that x cannot be NULL.
In the case of the following example, 2 is a valid input to example() and the function executes without error:
example(x = 2)
#> [1] 4However, if the user does not specify the x argument, an informative error message is returned along with instructions for how to rectify the issue:
example()
#> Error in `example()`:
#> ! `x` must be provided but is missing.
#> ℹ Add an argument for `x` to the function call.Likewise, if x is incorrectly specified (such as with a class(x) == character value), a similar message along with instructions for fixing the error is returned:
example(x = "test")
#> Error in `example()`:
#> ! `x` must be a <numeric> scalar, not a <character> scalar.
#> ℹ Provide a <numeric> scalar for `x`, such as `x = 1`.Additional Resources
Check out the vignettes and reference page for additional resources on using disputeR in your own projects as well as developing packages that contain disputeR checks.
Code of Conduct
Please note that the disputeR project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.