Conditionals, errors, tests, debugging Steve Bagley - - PowerPoint PPT Presentation

conditionals errors tests debugging
SMART_READER_LITE
LIVE PREVIEW

Conditionals, errors, tests, debugging Steve Bagley - - PowerPoint PPT Presentation

Conditionals, errors, tests, debugging Steve Bagley somgen223.stanford.edu 1 Conditional execution somgen223.stanford.edu 2 Writing a function to compute the sign of a number Lets write a function to compute the sign of a number:


slide-1
SLIDE 1

Conditionals, errors, tests, debugging

Steve Bagley

somgen223.stanford.edu 1

slide-2
SLIDE 2

Conditional execution

somgen223.stanford.edu 2

slide-3
SLIDE 3

Writing a function to compute the sign of a number

  • Let’s write a function to compute the sign of a number:
  • Return -1 if the number is < 0.
  • Return 0 if the number is 0.
  • Return 1 if the number is > 0.

somgen223.stanford.edu 3

slide-4
SLIDE 4

if, simple version

  • if is a basic control flow construct in R. It is not the same as ifelse.
  • Simple version: if(x == 0) 0
  • The simple version takes a test, x == 0, and an expression, 0.
  • If the test is true, the value of the expression is returned.
  • If the test is false, NULL is returned.

somgen223.stanford.edu 4

slide-5
SLIDE 5

compute_sign version 1

compute_sign1 <- function(x) { if(x < 0) -1 }

  • What is the result of compute_sign1(-2)?
  • What is the result of compute_sign1(2)?

somgen223.stanford.edu 5

slide-6
SLIDE 6

compute_sign version 2

compute_sign2 <- function(x) { if(x < 0) -1 if(x == 0) 0 }

  • What is the result of compute_sign2(-2)?

somgen223.stanford.edu 6

slide-7
SLIDE 7

if, complex version

if(x < 0) {

  • 1

} else { 1 }

  • If x < 0, this returns -1.
  • Else (because x >= 0), this returns 1.
  • if ... then ... else is a very common programming idiom.
  • Important: put the } and else on the same line.

somgen223.stanford.edu 7

slide-8
SLIDE 8

Braces { }

{ print(1) x <- 2 x + 1 } [1] 1 [1] 3

  • Braces { } can contain one or more R expressions, one per line.
  • The value of the last expression will be returned, unless you have done

something that alters control flow.

somgen223.stanford.edu 8

slide-9
SLIDE 9

compute_sign version 3

compute_sign3 <- function(x) { if(x < 0) {

  • 1

} else { 0 } }

  • What is the result of compute_sign3(-2)?
  • What is the result of compute_sign3(2)?

somgen223.stanford.edu 9

slide-10
SLIDE 10

compute_sign version 4

compute_sign4 <- function(x) { if(x < 0) {

  • 1

} else { if(x == 0) { } else { 1 } } } compute_sign4(-2) [1] -1 compute_sign4(0) [1] 0 compute_sign4(2) [1] 1

somgen223.stanford.edu 10

slide-11
SLIDE 11

What about values that are not numbers?

compute_sign4("abc") [1] 1

  • If the user supplies an argument that is not a number, we’d like to get an error,

not an incorrect result.

somgen223.stanford.edu 11

slide-12
SLIDE 12

Errors, warnings

somgen223.stanford.edu 12

slide-13
SLIDE 13

stop

  • The stop function raises an error. This interrupts the regular flow of
  • computation. It takes an argument, the error message that you want printed.

somgen223.stanford.edu 13

slide-14
SLIDE 14

compute_sign version 5

compute_sign5 <- function(x) { if(!is.numeric(x)) { stop("Hey, x is not a number.") } if(x < 0) {

  • 1

} else { if(x == 0) { } else { 1 } } } compute_sign5(-2) [1] -1 compute_sign5("abc") Error in compute_sign5("abc"): Hey, x is not a number.

somgen223.stanford.edu 14

slide-15
SLIDE 15

warning

  • warning prints a message, but the computation continues without interruption.

somgen223.stanford.edu 15

slide-16
SLIDE 16

compute_sign version 6

compute_sign6 <- function(x) { if(!is.numeric(x)) { warning("Hey, x is not a number.") } if(x < 0) {

  • 1

} else { if(x == 0) { } else { 1 } } } compute_sign6(-2) [1] -1 compute_sign6("abc") Warning in compute_sign6("abc"): Hey, x is not a number. [1] 1

somgen223.stanford.edu 16

slide-17
SLIDE 17

Assertions

  • Assertions are conditions that you expect to be true at a certain point in the code.
  • You can put if statements with stop to make sure that a condition is true, and

cause an error if it is not true.

  • There are several R packages for writing assertions that make this kind of

checking slightly easier.

somgen223.stanford.edu 17

slide-18
SLIDE 18

package assertthat

library(assertthat) x <- 2 assert_that(x > 0) [1] TRUE assert_that(x < 0) Error: x not less than 0

  • assert_that takes an expression, a test.
  • If it is TRUE then the function does nothing.
  • If it is FALSE then the function causes an error with a readable error message.
  • You can put assertions throughout your functions and scripts.

somgen223.stanford.edu 18

slide-19
SLIDE 19

compute_sign version 7

compute_sign7 <- function(x) { assert_that(is.numeric(x)) if(x < 0) {

  • 1

} else { if(x == 0) { } else { 1 } } } compute_sign7(-2) [1] -1 compute_sign7("abc") Error: x is not a numeric or integer vector

somgen223.stanford.edu 19

slide-20
SLIDE 20

Debugging

somgen223.stanford.edu 20

slide-21
SLIDE 21

Programs have bugs

  • All long, and many short, programs have bugs in them.
  • Nearly always an error is detected at some point after the initial mistake.

Working backwards from the error message to the ultimate site of the error is an acquired skill.

  • Defensive programming uses assertions to make sure that data have the proper

structure and values. (Eg, weight should be > 0.)

somgen223.stanford.edu 21

slide-22
SLIDE 22

Using print for debugging in R

  • The simplest way to debug is to put print statements in your code, showing

intermediate values.

somgen223.stanford.edu 22

slide-23
SLIDE 23

Using print in a pipe

iris %>% as_tibble() %>% print() %>% filter(Petal.Length > 1.5) %>% print()

  • print does the printing, but also returns its argument as the result.

somgen223.stanford.edu 23

slide-24
SLIDE 24

Super fancy way using View

library(magrittr) iris %>% ## note use of Tee pipe: %T>% as_tibble() %T>% View() %>% filter(Petal.Length > 1.5) %T>% View()

  • View does not return its argument as the result, so you need to work around that

using the %T>% operator from the package magrittr. This does the function on the right, but then returns the value on the left.

somgen223.stanford.edu 24

slide-25
SLIDE 25

Debugging in RStudio

  • RStudio has some powerful and easy to use debugging features.
  • Download https://somgen223.stanford.edu/files/debug_demo.r
  • Then open that file in the script window.
  • (live demo in RStudio)

somgen223.stanford.edu 25

slide-26
SLIDE 26

How to set a breakpoint

  • Click in the left margin at the line where you want to pause execution.
  • Click again in the same place to remove the breakpoint.

somgen223.stanford.edu 26

slide-27
SLIDE 27

Commands inside a breakpoint

Command Action c continue running code Q quit running variable name print value of variable

somgen223.stanford.edu 27

slide-28
SLIDE 28

Reading

  • Read: 19 Functions | R for Data Science (section 19.4 conditional execution)
  • Read: 19 Functions | R for Data Science (section 19.5.2 checking values)
  • Optional/advanced: Chapter 8 Testing and Error Handling | The Tidynomicon

somgen223.stanford.edu 28