Backtracing R Code for Troubleshooting

Follow

Sometimes when encountering an issue in R it is helpful to see precisely where in the compilation process the issue is encountered. 

For this, users can run a backtrace of any errors running R code. To capture the backtrace, submit the below two lines via the console prior to running the erroneous R code. 

> rlang::global_entrace()
> options(rlang_backtrace_on_error = "full")

Note: this command will only show output when an error is encountered. 

The backtrace output will appear in the R console like below:

> rlang::global_entrace()
> options(rlang_backtrace_on_error = "full")
> 
> lm(y ~ x , data = mtcars)
Error:
! object 'y' not found
Backtrace:
     1. ├─stats::lm(y ~ x, data = mtcars)
 2. │ └─base::eval(mf, parent.frame())
 3. │   └─base::eval(mf, parent.frame())
 4. ├─stats::model.frame(formula = y ~ x, data = mtcars, drop.unused.levels = TRUE)
 5. └─stats::model.frame.default(formula = y ~ x, data = mtcars, drop.unused.levels = TRUE)
 6.   └─base::eval(predvars, data, env)
 7.     └─base::eval(predvars, data, env)

This additional diagnostic output can be helpful to review when troubleshooting R issues or when submitting a support ticket.

 

Comments