within_bounds {assertr} R Documentation

## Creates bounds checking predicate

### Description

This function returns a predicate function that will take a numeric value or vector and return TRUE if the value(s) is/are within the bounds set. This does not actually check the bounds of anything–it only returns a function that actually does the checking when called with a number. This is a convenience function meant to return a predicate function to be used in an assertr assertion.

### Usage

within_bounds(
lower.bound,
upper.bound,
include.lower = TRUE,
include.upper = TRUE,
allow.na = TRUE,
check.class = TRUE
)


### Arguments

 lower.bound The lowest permitted value upper.bound The upper permitted value include.lower A logical indicating whether lower bound should be inclusive (default TRUE) include.upper A logical indicating whether upprt bound should be inclusive (default TRUE) allow.na A logical indicating whether NAs (including NaNs) should be permitted (default TRUE) check.class Should the class of the lower.bound, upper_bound, and the input to the returned function be checked to be numeric or of the same class? If FALSE, the comparison may have unexpected results.

### Value

A function that takes numeric value or numeric vactor and returns TRUE if the value(s) is/are within the bounds defined by the arguments supplied by within_bounds and FALSE otherwise

### Examples

predicate <- within_bounds(3,4)
predicate(pi)

## is equivalent to

within_bounds(3,4)(pi)

# a correlation coefficient must always be between 0 and 1
coeff <- cor.test(c(1,2,3), c(.5, 2.4, 4))[["estimate"]]
within_bounds(0,1)(coeff)

## check for positive number
positivep <- within_bounds(0, Inf, include.lower=FALSE)

## this is meant to be used as a predicate in an assert statement
assert(mtcars, within_bounds(4,8), cyl)

## or in a pipeline

library(magrittr)

mtcars %>%
assert(within_bounds(4,8), cyl)



[Package assertr version 3.0.1 Index]