SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
Stat405              Debugging


                           Hadley Wickham
Monday, 23 November 2009
Pop quiz
                  Which is the best way to solve a problem?
                  Write a giant function that tries to do
                  everything, and then when it doesn’t work
                  you have no idea where the problem is.
                  Write small functions that each do a single
                  task and can be tested easily. If there is a
                  problem, you can localise it to a few lines
                  of code


Monday, 23 November 2009
Tools of
              last resort
Monday, 23 November 2009
If you’re using
                  them all the time,
                     something is
                   wrong with your
                   basic approach
Monday, 23 November 2009
traceback()
                             browser()
                             recover()

                           options(error)

Monday, 23 November 2009
f <- function(x) {
        h(x)

           i(x)
     }
     g <- function(x) {
       a <- 10
       x
     }
     h <- function(x) {
       i(x)
       i(x)
     }
     i <- function(x) {
       if (sample(5, 1) == 1) stop("This is an error!")
     }

     f()
     traceback()           # This is the call stack

Monday, 23 November 2009
Browser
                  Creates an interactive prompt in a function’s
                  environment
                  Four special commands (no brackets):
                           n = next line
                           c = continue (or just press return)
                           where = where am I in the call stack?
                           Q = quit
                  Can also use any regular R function

Monday, 23 November 2009
j <- function(x, y = 10) {
            k(x, y)
        }


        k <- function(x, y) {
            z <- 3
            browser()
            x + y
        }


        j(10)




Monday, 23 November 2009
Your turn

                  Familiarise yourself with browser()
                  Try using ls() while you are browsing.
                  What do you see?
                  Try modifying the values inside the
                  function. What happens to the result?



Monday, 23 November 2009
Browser
                  debug(f) automatically adds a browser
                  statement to the start of f, undebug(f)
                  removes it.
                  debugonce(f) automatically removes it after
                  f is run for the first time
                  If the error occurs only under some
                  conditions you might want to put browser()
                  inside an if statement.


Monday, 23 November 2009
Recover

                  Works like browser(), but lets you jump
                  anywhere in the call stack
                  Most usefully:
                  options(error = recover)




Monday, 23 November 2009
# Can change the default behaviour when an error occurs
        options(error = recover)
        f()


        # Set to NULL to return to the default (i.e. do nothing)
        options(error = NULL)


        # Another useful option: turn warnings into errors
        options(warn = 2)
        # and turn them back
        options(warn = 0)




Monday, 23 November 2009
Your turn

                  Use options(error = recover) and
                  explore the call stack of f.
                  Use ls() to explore what variables are
                  defined in each environment




Monday, 23 November 2009
# Your turn
        # Use the tools you have just learned about to debug
        # this function and create a version that works


        larger <- function(x, y) {
            y.is.bigger <- y > x
            x[y.is.bigger] <- y[y.is.bigger]
            x
        }
        larger(c(1, 5, 10), c(2, 4, 11))
        larger(c(1, 5, 10), 6)
        larger(c(1, 5, 10), c(2, 4, NA))




Monday, 23 November 2009
Learn more


                  http://www.biostat.jhsph.edu/~rpeng/
                  docs/R-debug-tools.pdf




Monday, 23 November 2009

Weitere ähnliche Inhalte

Ähnlich wie 25 Debugging

Devon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascriptDevon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascript
Daum DNA
 
CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...
CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...
CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...
CodeFest
 

Ähnlich wie 25 Debugging (13)

Project Fortress
Project FortressProject Fortress
Project Fortress
 
Devon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascriptDevon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascript
 
Vim Notes
Vim NotesVim Notes
Vim Notes
 
Defensive programming in Javascript and Node.js
Defensive programming in Javascript and Node.jsDefensive programming in Javascript and Node.js
Defensive programming in Javascript and Node.js
 
10 simulation
10 simulation10 simulation
10 simulation
 
10 simulation
10 simulation10 simulation
10 simulation
 
What to do when things go wrong
What to do when things go wrongWhat to do when things go wrong
What to do when things go wrong
 
CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...
CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...
CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...
 
21 Polishing
21 Polishing21 Polishing
21 Polishing
 
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
 
R Debugging
R DebuggingR Debugging
R Debugging
 
Macruby - RubyConf Presentation 2010
Macruby - RubyConf Presentation 2010Macruby - RubyConf Presentation 2010
Macruby - RubyConf Presentation 2010
 
Acceptance & Integration Testing With Behat (PBC11)
Acceptance & Integration Testing With Behat (PBC11)Acceptance & Integration Testing With Behat (PBC11)
Acceptance & Integration Testing With Behat (PBC11)
 

Mehr von Hadley Wickham (20)

27 development
27 development27 development
27 development
 
27 development
27 development27 development
27 development
 
24 modelling
24 modelling24 modelling
24 modelling
 
23 data-structures
23 data-structures23 data-structures
23 data-structures
 
Graphical inference
Graphical inferenceGraphical inference
Graphical inference
 
R packages
R packagesR packages
R packages
 
22 spam
22 spam22 spam
22 spam
 
21 spam
21 spam21 spam
21 spam
 
20 date-times
20 date-times20 date-times
20 date-times
 
19 tables
19 tables19 tables
19 tables
 
18 cleaning
18 cleaning18 cleaning
18 cleaning
 
17 polishing
17 polishing17 polishing
17 polishing
 
16 critique
16 critique16 critique
16 critique
 
15 time-space
15 time-space15 time-space
15 time-space
 
14 case-study
14 case-study14 case-study
14 case-study
 
13 case-study
13 case-study13 case-study
13 case-study
 
12 adv-manip
12 adv-manip12 adv-manip
12 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
09 bootstrapping
09 bootstrapping09 bootstrapping
09 bootstrapping
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Kürzlich hochgeladen (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

25 Debugging

  • 1. Stat405 Debugging Hadley Wickham Monday, 23 November 2009
  • 2. Pop quiz Which is the best way to solve a problem? Write a giant function that tries to do everything, and then when it doesn’t work you have no idea where the problem is. Write small functions that each do a single task and can be tested easily. If there is a problem, you can localise it to a few lines of code Monday, 23 November 2009
  • 3. Tools of last resort Monday, 23 November 2009
  • 4. If you’re using them all the time, something is wrong with your basic approach Monday, 23 November 2009
  • 5. traceback() browser() recover() options(error) Monday, 23 November 2009
  • 6. f <- function(x) { h(x) i(x) } g <- function(x) { a <- 10 x } h <- function(x) { i(x) i(x) } i <- function(x) { if (sample(5, 1) == 1) stop("This is an error!") } f() traceback() # This is the call stack Monday, 23 November 2009
  • 7. Browser Creates an interactive prompt in a function’s environment Four special commands (no brackets): n = next line c = continue (or just press return) where = where am I in the call stack? Q = quit Can also use any regular R function Monday, 23 November 2009
  • 8. j <- function(x, y = 10) { k(x, y) } k <- function(x, y) { z <- 3 browser() x + y } j(10) Monday, 23 November 2009
  • 9. Your turn Familiarise yourself with browser() Try using ls() while you are browsing. What do you see? Try modifying the values inside the function. What happens to the result? Monday, 23 November 2009
  • 10. Browser debug(f) automatically adds a browser statement to the start of f, undebug(f) removes it. debugonce(f) automatically removes it after f is run for the first time If the error occurs only under some conditions you might want to put browser() inside an if statement. Monday, 23 November 2009
  • 11. Recover Works like browser(), but lets you jump anywhere in the call stack Most usefully: options(error = recover) Monday, 23 November 2009
  • 12. # Can change the default behaviour when an error occurs options(error = recover) f() # Set to NULL to return to the default (i.e. do nothing) options(error = NULL) # Another useful option: turn warnings into errors options(warn = 2) # and turn them back options(warn = 0) Monday, 23 November 2009
  • 13. Your turn Use options(error = recover) and explore the call stack of f. Use ls() to explore what variables are defined in each environment Monday, 23 November 2009
  • 14. # Your turn # Use the tools you have just learned about to debug # this function and create a version that works larger <- function(x, y) { y.is.bigger <- y > x x[y.is.bigger] <- y[y.is.bigger] x } larger(c(1, 5, 10), c(2, 4, 11)) larger(c(1, 5, 10), 6) larger(c(1, 5, 10), c(2, 4, NA)) Monday, 23 November 2009
  • 15. Learn more http://www.biostat.jhsph.edu/~rpeng/ docs/R-debug-tools.pdf Monday, 23 November 2009