Shannon Pileggi
Getting started
Troubleshooting
Moving on
Debugging your code
Debugging in RStudio
Debugging their code
Special cases
Read the source
Licensing
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License (CC BY-SA4.0).
Material
This material was originally developed as part of the 2-day RStudio Conference “What They Forgot To Teach You About R” workshop.
R installed? Pretty recent?
Recommended R ≥ 4.2.0
RStudio installed?
I’m on 2022.02.3+492
Ready to build packages?
devtools::has_devel()
Your system is ready to build packages!
WTF Ch 11 Debugging R code
https://rstats.wtf/debugging-r-code.html
Advanced R Ch 22 Debugging
https://adv-r.hadley.nz/debugging.html
Jenny Bryan 2020 RStudio Conf Keynote Object of type closure is not subsettable
https://github.com/jennybc/debugging#readme
Amanda Gadrow 2018 Webinar Debugging techniques in RStudio
https://www.rstudio.com/resources/webinars/debugging-techniques-in-rstudio/
Jim Hester 2019 Introduction to debugging in R and RStudio
https://www.jimhester.com/talk/2019-crug-debugging/
Maëlle Salmon 2021 How to become a better R code detective?
https://masalmon.eu/2021/07/13/code-detective/
Kara Woo 2019 RStudio Conf Box plots A case study in debugging and perseverance https://www.rstudio.com/resources/rstudioconf-2019/box-plots-a-case-study-in-debugging-and-perseverance/
Google exact error message
keyword search on RStudio community
keyword search on stackoverflow, [r]
tag
Samantha Csik 2022 R-Ladies St. Louis Workshop Teach Me How To Google: slides, recording
Have you tried turning it OFF
and ON
again?
Restart R, especially when things get weird.
Session -> Restart R or
Ctrl + Shift + F10 (Windows),
Cmd + Shift + 0 / Cmd + Shift + F10 (Mac)
Tools -> Global Options -> Workspace
Click here to open the NHS-R RStudio Cloud debugging work space.
In RStudio Cloud, set your Workspace options as shown.
Tools -> Global Options -> Workspace
03:00
minimum repr
oducible ex
ample
make a reprex….. please by Sharla Gelfand (2021)
slides: https://make-a-reprex-please.netlify.app/#1
recording: https://www.youtube.com/watch?v=G5Nm-GpmrLw
Jenny Bryan (2020) Object of type closure is not subsettable
repr
oducible ex
ampleJenny Bryan (2020) Object of type closure is not subsettable
Jenny Bryan (2020) Object of type closure is not subsettable
Troubleshooting strategies:
Search
Reset
Reprex
from troubleshooting to…
formal debugging
techniques.
traceback
location
where did the error occur
interactive debugger
context
why did the error occur
your code vs their code
This is a lot.
These tools achieve similar objectives slightly differently.
People don’t generally use all of these tools at once. They pick and choose the ones they like.
⚠️ name your script with functions
⚠️ source your script with functions
for the best debugging experience 😎
shows the sequence of calls that lead to the error.
the trace back is also known as:
call stack, stack trace, & back trace
options(error = rlang::entrace)
could go in your .Rprofile
Numbering and ordering differs between traceback()
and rlang functions.
browser()
opens the interactive debugger.
Modify the function by inserting a browser()
statement.
Source the function.
Execute the function.
Investigate objects
Control execution
ls()
, ls.str()
,
str()
, print()
command | operation |
---|---|
n |
next statement |
c |
continue (leave interactive debugging) |
s |
step into function call |
f |
finish loop / function |
where |
show previous calls |
Q |
quit debugger |
Click here to open the NHS-R RStudio Cloud debugging work space.
Complete 01_exercise to practice debugging your own code.
Check out the README.md
for some getting started tips!
01_debugging_spartan.R
(directions to explore without suggested code)
01_debugging_comfy.R
(directions to explore with suggested code)
01_debugging_solution.R
(directions to explore with code solutions)
15:00
red circle = breakpoint = browser()
(but you don’t have to change your code)
Set / re-set an editor breakpoint:
click to the left of the line number in the source file
press Shift+F9 with your cursor on the line
red circle = breakpoint = browser()
(but you don’t have to change your code)
To activate, either
click IDE Source button, or
debugSource(
“demo/my_functions.R”)
RStudio IDE cheatsheet
# IDE Error Inspector not triggered
f <- function(x) x + 1
g <- function(x) f(x)
g("a")
# IDE Error Inspector not triggered
strsplit(factor("a,b"), ",")
# IDE Error Inspector not triggered
f <- function(x) strsplit(x, ",")
f(factor("a,b"))
# IDE Error Inspector yes triggered
g <- function(x) f(x)
g(factor("a,b"))
The error inspector is only invoked if your
code
is involved.
To have RStudio invoke the debugger on any error,
Tools -> Global Options
uncheck Use debug error handler only when my code contains errors.
Complete 02_exercise to practice debugging a slightly different version of your own code, using features from the RStudio IDE.
07:00
debug()
= browser()
/ breakpoint
in first line of function
interactive debugger is initiated every time g()
is executed, until undebug("g")
depending on function internals, this can… trap you in the debugger 😬
debugonce()
= browser()
/ breakpoint
in first line of function
one time only!
interactive debugger initiated a single time when g()
is executed
we already discussed
options(error = rlang::entrace)
for a richer traceback on error
🤠 options(error = recover)
displays an interactive prompt with frames
you select the frame to enter the debugger
trace(what = fun, tracer = browser)
is equivalent to
inserting browser()
in first line of function
debug(fun)
you an also insert any code at any location in function
trace(what = fun, tracer = browser, at = 2)
browser()
at second step of fun
untrace(fun)
cancels the tracing
trace(what = colSums, tracer = browser)
is equivalent to
inserting browser()
in first line of colSums
if we had the source code
debug(colSums)
investigate 🧐 the function with as.list()
+ body()
Complete 03_exercise to practice debugging others’ code.
10:00
rmarkdown chunk option error = TRUE
enables knitting with errors
insert knitr::knit_exit()
and interactively work through .Rmd
Adv R Ch 22.5.3 RMarkdown https://adv-r.hadley.nz/debugging.html#rmarkdown
WTF Ch 11.4 Debugging in Rmarkdown documents https://rstats.wtf/debugging-r-code.html#debugging-in-r-markdown-documents
tracebacks can be verbose with pipes
gives trimmed tracebacks when using pipes
Matt Dray 2019 blog post Fix leaky pipes in R
If you want to dig deeper into a warning, you can convert them to errors to initiate debugging tools.
?options
options(warn = 0) # default, stores warnings until top-level function returns
options(warn = 1) # warnings are printed as they occur
options(warn = 2) # upgrades warnings to errors
# initiate recover on warning
options(warn = 2, error = recover)
# restore original settings
options(warn = 0, error = NULL)
Link | Description |
---|---|
github.com/search | All of GitHub! Can do advanced searches, too. |
github.com/cran | CRAN packages |
github.com/wch/r-source | R core |
In R console
pkg::fnc
to see exported functions
pkg:::fnc
to see non-exported functions
From Package development with devtools RStudio cheatsheet
https://www.rstudio.com/resources/cheatsheets/
At a high level,
how does readr::read_lines() work?
03:00
Location | Item |
---|---|
src/main | base R |
src/library/ | packages included in base R |
doc/manual | R manuals & documentation |
When was the trimws() function added to base R?
How does is.na() work?
Hints on next slides.
05:00
Try restricting search to only commit messages in github.com/wch/r-source.
Look in github.com/wch/r-source/src/main/names.c to find the name of the internal function. Or in GitHub search bar: is.na filename:names.c
.
In GitHub search bar: "do_isna" language:c
Troubleshoot
Debug
Read the source