


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















D E F E N S I V E R P R O G R A M M I N G Dr. Colin Gillespie Jumping Rivers
Avoid problems where possible Handle issues as they arise in a sensible way As an example, using the shortcuts T / F for TRUE / FALSE
Suppose we are working out an F-statistic. It would be natural to have
(F <- df( 1 , 9 , 67 )) [1] 0. But R treats positive numbers as TRUE , so if(F) message("Yer aff yer heid!") Yer aff yer heid! F is now treated as TRUE!
Get in the habit of using TRUE and FALSE not T and F If you testing for TRUE , use isTRUE() isTRUE(T) [1] TRUE isTRUE( 2 ) [1] FALSE T <- 10 isTRUE(T) [1] FALSE
D E F E N S I V E R P R O G R A M M I N G Dr. Colin Gillespie Jumping Rivers
Signals to the user the state of a process This isn't an error - it's just helpful information For example, suppose you're running cross-validation, then output could be CV 1 of 10 complete CV 2 of 10 complete CV 3 of 10 complete
Occasionally, packages can be a bit noisy Sometimes loading ggplot 2 , it presents a message Don't worry, we can tell it to be quiet suppressPackageStartupMessages(library("ggplot2"))
The message() function is helpful for le ing you and other users know what's happening. It's very handy for long running processes
D E F E N S I V E R P R O G R A M M I N G Dr. Colin Gillespie Jumping Rivers
The warning() function warning("You have been warned!")
signals that something may have gone wrong R continues (unlike an error) "Warning message:" is (pre) appended
Suppose we're performing regression on d = data.frame(y = 1 : 4 , x1 = 1 : 4 ) d$x2 = d$x1 + 1 So x2 = x1 + 1 When we t a multiple linear regression model m = lm(y ~ x1 + x2, data = d) summary(m)
D E F E N S I V E R P R O G R A M M I N G Dr. Colin Gillespie Jumping Rivers
Sometimes things are just broken We need to raise an error For example: 1 + "stuff" Error in 1 + "stuff": non-numeric argument to binary operator