Exercise

Custom error logic

Sometimes the assert_*() functions in assertive don't give the most informative error message. For example, the assertions that check if a number is in a numeric range will tell the user that a value is out of range, but the won't say why that's a problem. In that case, you can use the is_*() functions in conjunction with messages, warnings, or errors to define custom feedback.

The harmonic mean only makes sense when x has all positive values. (Try calculating the harmonic mean of one and minus one to see why.) Make sure your users know this!

Instructions

100 XP
  • If any values of x are non-positive (ignoring NAs) then throw an error.
  • Look at what happens when you pass a character argument to calc_harmonic_mean().