assertr (version 2.7)

within_bounds: 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
)

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)

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

Run this code
# NOT RUN {
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)

# }

Run the code above in your browser using DataCamp Workspace