Skip to content

Instantly share code, notes, and snippets.

@moritzpschwarz
Last active September 19, 2022 10:50
Show Gist options
  • Save moritzpschwarz/64905c338dfa4307618d33999e6e8d9e to your computer and use it in GitHub Desktop.
Save moritzpschwarz/64905c338dfa4307618d33999e6e8d9e to your computer and use it in GitHub Desktop.
A small collection of debugging tools in R
# to get to the recover list - allows you to walk through all frames
options(error = recover)
# to get back from a call to the options page again, use `c`
# just loading any library to try these functions out
library(gets)
# trace will execute any function when you execute the first function
# e.g. it will execute `browser` when I execute `getsm`
trace(getsm, browser)
# But I can also tell it to execute at a certain point
# To figure out which expression is which number, you can look at the list e.g. through this
# as.list(body(fun))
# In our case:
as.list(body(getsm))
# Then the function looks like this:
trace(getsm, browser, at = 5)
# To debug a function that is an S3 method or a hidden function, you also need to specfiy the 'where' argument
trace(what = isat.default, tracer = browser, at = 10, where = gets)
getsm(arx(y = rnorm(100), mc = TRUE))
# To stop tracing this
untrace(getsm)
# debug
# debug will simply open a browser straight away when I execute it
debug(isat)
data(Nile)
isat(Nile)
# stop behaviour
undebug(isat)
# traceback
# to get a full historythe command
traceback()
# Change only one function in a package with your custom function
environment(create.ncdf.output.files.custom) <- asNamespace('climdex.pcic.ncdf')
assignInNamespace("create.ncdf.output.files", create.ncdf.output.files.custom, ns = "climdex.pcic.ncdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment