R seems to take a very laissez-faire stance towards error handling, with behavior similar to most shell scripting languages. On top of that, R tends to print plenty of innocuous text to the terminal. This makes it easy for errors to go unnoticed. More than once I've had a script generate errors early on and then happily propagate NaNs through the rest of the calculation. Only after puzzling over the bizarre results for quite a while did I realize what had happened.
How do experienced R users deal with this? Is there a way to make errors fatal or at least more noticeable?
I know that one thing some people do is to run R with all warnings set to throw errors:
options(warn = 2)
> x <- "a"
> as.numeric(x)
Error: (converted from warning) NAs introduced by coercion
> sqrt(-1)
Error in sqrt(-1) : (converted from warning) NaNs produced
Obviously, this is in part a matter of taste. But you should be aware that adopting this approach will also include all warnings from packages, where the authors may or may not have a reasonable definition of what ought to generate a warning. So you might run into code that generates lots of warnings that you actually don't care about.
Then you'd have to turn to wrapping expressions in suppressWarnings
to explicitly quiet the ones you decide don't matter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With