Advanced debugging functionality in R?

2019-05-04 17:52发布

问题:

Are there any packages and/or hacks in R that give the debugger more functionality? I'm specifically looking for:

  1. The ability to step over a whole loop
  2. The ability to step into function calls without calling debug on them also
  3. The ability to set breakpoints or effectively insert browser calls into the code while I am already in the debugger (i.e. so that if I figure out during debugging where I want a breakpoint I don't have to quit and rerun the whole function again)

Etc.

On edit: I run R out of emacs/ess, so if there's any ess trick that can help, I'd be interested in that also.

回答1:

This is called ess-tracebug. This and a bunch of other functionality for development is available on C-c C-t ess-dev-map. Press C-c C-t C-h to see what is there It should be self explanatory.

See this section of ess manual and the original project page of ess-tracebug for basic stuff. There are several breakpoint types available and you can a add your own breakpoints and make them execute arbitrary R code. Same for on-error actions and loggers.

There is also a tutorial there.

As to your points.

  1. You cannot jump over the whole loop by default (R doesn't allow that, afaik). But you can jump over several iterations with M-N or place a breakpoint after the loop and run M-C when you hit a loop (you need eval/source in advance though). There is also M-U to jump to the outer call frame.

  2. Yes, C-c C-t C-d to mark whatever function or method you want for a debugger. It is smart enough to also show internal/namespace functions visible from the current debugging context. See here for how it looks.

  3. You can either mark the function for debugging as in (2) above, or insert the breakpoint and evaluate the function. The effect of the evaluation will depend on how you do it. If it is a simple evaluation (like C-c C-c) then the function will be sourced into the current context, which might be what you want but most likely not. If developer is active and the function is part of the developed package(s), then the evaluation happens at namespace/package level, so you will get your breakpoints installed immediately.

You can also toggle breakpoints with C-c C-t o. The effect is immediate, you won't need to source/evaluate your function again.

To conclude, C-c C-t C-d is probably the cleanest way to debug a function/method if you already in the debugging context. Otherwise, just set a breakpoint and evaluate/source the code.

On the fly breakpoints might be added in the future, but it will add an additional layer of complexity with not much gain IMO.



回答2:

A visual debugger has since long been available within Architect (a stand-alone Eclipse-based R IDE) and StatET (Eclipse plugins for R development). Breakpoints, step in, step over etc. are obviously available and one can (within one instance) work with and debug multiple R sessions, both local and remote.



标签: r debugging ess