Skip to content

Instantly share code, notes, and snippets.

@gmbecker
Last active September 26, 2018 04:25
Show Gist options
  • Save gmbecker/e47b0951b91505b2f4607259bcec8f97 to your computer and use it in GitHub Desktop.
Save gmbecker/e47b0951b91505b2f4607259bcec8f97 to your computer and use it in GitHub Desktop.
DebuggingInRmd
We set up knitr so it doesn't catch errors, then set
`options(error=recover)` to set up R's debug-on-error machinery.
We have to do one additional thing, before the options call though:
trace the recover function with`sink(NULL)` to turn off the output
capturing so the R console is useful when the debugging framework
dumps us back into it. This has to happen before the options call
because that call grabs the `recover` object and stores it somewhere
so setting a trace on recover after the call won't affect the cached
version that gets called upon an error.
All of these steps (in this order) can happen in the R session rather
than the Rmd document, and generally should.
```{r init}
knitr::opts_chunk$set(error=FALSE)
trace(recover, quote(sink(NULL)))
options(error=recover)
```
Now we define a function and then break it. Our function will appear
near the bottom of the list of frames in the debugger, we can pretty
much ignore anything above it unless we are hunting a bug in knitr.
```{r brokestuff}
awesomefun = function(i) {
stopifnot(is(i, "integer"))
i
}
awesomefun("whaaaat")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment