SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à 25 Debugging

Project Fortress
Project FortressProject Fortress
Project FortressAlex Miller
 
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-javascriptDaum DNA
 
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.jsRuben Tan
 
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 wrongDorneles Treméa
 
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
 
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)Howard Lewis Ship
 
Macruby - RubyConf Presentation 2010
Macruby - RubyConf Presentation 2010Macruby - RubyConf Presentation 2010
Macruby - RubyConf Presentation 2010Matt Aimonetti
 
Acceptance & Integration Testing With Behat (PBC11)
Acceptance & Integration Testing With Behat (PBC11)Acceptance & Integration Testing With Behat (PBC11)
Acceptance & Integration Testing With Behat (PBC11)benwaine
 

Similaire à 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)
 

Plus de 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
 

Dernier

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Dernier (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

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