if (FALSE) {
f <- function(x, y) "Pass"
# Impose the condition that x is a formula
g <- firmly(f, vld_formula(~x))
g(z ~ a + b, 0) # [1] "Pass"
g(0, 0) # Error: "Not formula: x"
# Impose the condition that x and y are disjoint (assuming they are vectors)
h <- firmly(f, vld_empty(~intersect(x, y)))
h(letters[1:3], letters[4:5]) # [1] "Pass"
h(letters[1:3], letters[3:5]) # Error: "Not empty: intersect(x, y)"
# Use a custom error message
h <- firmly(f, vld_empty("x, y must be disjoint" ~ intersect(x, y)))
h(letters[1:3], letters[3:5]) # Error: "x, y must be disjoint"
# vld_true can be used to implement any kind of input validation
ifelse_f <- firmly(ifelse, vld_true(~typeof(yes) == typeof(no)))
(w <- {set.seed(1); rnorm(5)})
# [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078
ifelse_f(w > 0, 0, "1") # Error: "Not TRUE: typeof(yes) == typeof(no)"
ifelse_f(w > 0, 0, 1) # [1] 1 0 1 0 0
}
Run the code above in your browser using DataLab