0% found this document useful (0 votes)
10 views8 pages

Debugging

debugging
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Debugging

debugging
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Debugging

Roger D. Peng, Associate Professor of Biostatistics


Johns Hopkins Bloomberg School of Public Health
Something’s Wrong!
Indications that something’s not right

· message: A generic notification/diagnostic message produced by the message function;


execution of the function continues
· warning: An indication that something is wrong but not necessarily fatal; execution of the
function continues; generated by the warning function

· 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)

## Warning: NaNs produced

## [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)

## [1] "x is greater than zero"

printmessage(NA)

## Error: missing value where TRUE/FALSE needed

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)

## Warning: NaNs produced

printmessage2(x)

## [1] "x is a missing value!"

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?

· What were you expecting? Output, messages, other results?


· What did you get?
· How does what you get differ from what you were expecting?

· Were your expectations correct in the first place?


· Can you reproduce the problem (exactly)?

8/15

You might also like