4

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.

mrip
  • 14,913
  • 4
  • 40
  • 58
  • RStudio has very good debugging functionality catering to almost all 3 points mentioned. – CHP Oct 18 '13 at 16:41
  • @geektrader Thanks, that's good to know. I currently use emacs/ess. I remember when i looked at RStudio there was something i didn't like, but I don't remember what it was. This might push be to take a second look. – mrip Oct 18 '13 at 16:42
  • @geektrader Oh now I remember. IIRC you can only have one code window open (i.e. visible) at once, and the layout is pretty constrained to what they want you to do. Is it still that way? I like having lots of windows. – mrip Oct 18 '13 at 17:35
  • @geektrader Does RStudio support his #2? If so, how? – Kent Johnson Oct 18 '13 at 21:10
  • @mrip RStudio only shows one code window at a time. You can customize the layout a little but it is still pretty fixed. – Kent Johnson Oct 18 '13 at 21:12
  • If you use RStudio's Projects feature (which I recommend, as it allows you to keep your code and objects compartmentalized), then you can run multiple instances of RStudio no problem, each (obviously) with their own code window. – Twitch_City Oct 19 '13 at 00:09
  • 1
    Also, the latest RStudio beta was really focused on upgrading the debugging tools. See the changelog here: http://www.rstudio.com/ide/download/preview (as you might guess, I'm partial to RStudio) – Twitch_City Oct 19 '13 at 00:11

2 Answers2

3

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.

VitoshKa
  • 8,387
  • 3
  • 35
  • 59
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.