Exercise answers
set.seed(19790801)
library(assertive)
knitr::opts_chunk$set(error = FALSE)
1. Introduction
- Just type the code in the question!
example(assert_is_vector)
.example(assert_all_are_whole_numbers)
example(is_us_zip_code)
2. Checking function inputs
The answer is a little subjective.
For
x
, I suggest simply enforcing that it isnumeric
usingassert_is_numeric(x)
.center
andconstant
are expected to be single numbers. You can either throw an error if this is not the case usingassert_is_a_number(center)
, etc., or have a slightly more forgiving check for numeric followed by retrieving of the first value usingcenter <- use_first(center)
, etc.. For very strict checking, you may also wish to ensure thatcenter
andconstant
are not missing or NaN usingassert_all_are_not_na(c(center, constant))
.na.rm
,low
andhigh
are expected to be logical values. You can throw an error for inputs that don't conform usingassert_is_a_bool(na.rm)
, etc.. I suggest being more forgiving by extracting the first element and coercing to logical usingna.rm <- coerce_to(use_first(na.rm), "logical")
, etc.