Skip to content

Instantly share code, notes, and snippets.

@jennybc
Forked from gmbecker/broken.Rmd
Created September 2, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennybc/47ebc68cb5ffa366b8e2ce1a9ebcd7d7 to your computer and use it in GitHub Desktop.
Save jennybc/47ebc68cb5ffa366b8e2ce1a9ebcd7d7 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