within_bounds: Creates bounds checking predicate

Description Usage Arguments Value Examples

View source: R/predicates.R

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

1
2
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)

lorenzwalthert/assertr documentation built on May 20, 2019, 4:06 p.m.