## Not run: 
# 
# ensure_that(1:10, is.integer)
# 
# # Examples below will use the magrittr pipe
# library(magrittr)
# 
# # Create a contract which can ensure that a matrix is square.
# ensure_square <- ensures_that(NCOL(.) == NROW(.))
# 
# # apply it.
# A <-
#   diag(4) %>%
#   ensure_square
# 
# # Without the pipe operator:
# A <- ensure_square(diag(4))
# 
# # Ensure on the fly (this will pass the test)
# A <-
#   matrix(runif(16), 4, 4) %>%
#   ensure_that(ncol(.) == nrow(.), all(. <= 1))
# 
# # This will raise an error
# A <-
#   matrix(NA, 4, 4) %>%
#   ensure_that(. %>% anyNA %>% not)
# 
# # Tweak failure:
# A <-
#   1:10 %>%
#   ensure_that(all(. < 5), err_desc = "Number tests!")
# 
# # A default value for failure situations:
# A <-
#   1:10 %>%
#   ensure_that(all(. < 5), fail_with = NA)
# 
# # Suppose you had an email function:
# email_err <- function(e) {email(e$message); stop(e)}
# 
# A <-
#   1:10 %>%
#   ensure_that(all(. < 5), fail_with = email_err)
# 
# # Two similar contracts, one extending the other.
# # Note also that custom message is used for A
# A <- ensures_that(all(.) > 0 ~ "Not all values are positive")
# B <- ensures_that(!any(is.na(.)) ~ "There are missing values", +A)
# 
# B(c(-5:5, NA))
# ## End(Not run)
Run the code above in your browser using DataLab