valid <- c(1, 2, 3)
x <- 2L
check_content(x, valid)
if (FALSE) {
# `x` must have the same type with `valid`
x <- "a"
check_content(x, valid)
# `x` must have length 1
x <- c(1, 2)
check_content(x, valid)
# differentiate between type double and integer
x <- 2L
check_content(x, valid, as_double = FALSE)
# `valid` can be a function
check_content(x, is.na, general = "`x` must be `NA`.")
# `valid` can be a string of R code
check_content(x, "is.na(x)", general = "`x` must be `NA`.")
}
Run the code above in your browser using DataLab