Debugging
Debugging
· error: An indication that a fatal problem has occurred; execution stops; produced by the stop
function
· condition: A generic concept for indicating that something unexpected can occur; programmers
can create their own conditions
2/15
Something’s Wrong!
Warning
log(-1)
## [1] NaN
3/15
Something’s Wrong
printmessage <- function(x) {
if(x > 0)
print("x is greater than zero")
else
print("x is less than or equal to zero")
invisible(x)
}
4/15
Something’s Wrong
printmessage <- function(x) {
if (x > 0)
print("x is greater than zero") else print("x is less than or equal to zero")
invisible(x)
}
printmessage(1)
printmessage(NA)
5/15
Something’s Wrong!
printmessage2 <- function(x) {
if(is.na(x))
print("x is a missing value!")
else if(x > 0)
print("x is greater than zero")
else
print("x is less than or equal to zero")
invisible(x)
}
6/15
Something’s Wrong!
printmessage2 <- function(x) {
if (is.na(x))
print("x is a missing value!") else if (x > 0)
print("x is greater than zero") else print("x is less than or equal to zero")
invisible(x)
}
x <- log(-1)
printmessage2(x)
7/15
Something’s Wrong!
How do you know that something is wrong with your function?
· What was your input? How did you call the function?
8/15