Lucky
Lucky

Reputation: 347

line by line debugging in R studio

Is there a way to debug the code line by line in R studio environment ??

I know there are breakpoints, Next, continue etc to debug. But I am looking for a line by line debug option like the one in Visual Studio.

Thank you

Upvotes: 5

Views: 8570

Answers (2)

Stefan
Stefan

Reputation: 12370

For R-Studio newbies like me that are used to other IDEs:

a) Set a break point by clicking on the border or pressing Shift+F9 (=>red break point dot is shown)

b) As equivalent to "Debug" in other IDEs:

  • Click source or
  • Press Ctrl+Shift+Enter or
  • Activate source on save and save

c) Have a look at the Console view. There are common debug options:

  • Execute Next Line F10
  • Step Into Function Shift+F4
  • Finish function Shift+F6
  • Continue Shift+F5
  • Stop Debugging Shift+F8

(Unfortunately I did not find a way to adapt the key shortcuts for those options. They are not listed under Tools=>Modify Keyboard shortcuts.)

enter image description here

d) There does not seem to be a "hover over expression" feature while debugging. You can have a look at the Environment view to see the value of a variable and use the Console to evaluate expressions while debugging.

If you want to run the script without debugging and without clearing the break points, select all lines Ctrl+A and use the Run button. (Seems to be complicated to me.... I would expect an extra run button or key shortcut for that but could not find one.)

If there is no selection, the Run button only executes the current line. You can press that button several times to step through the code and see the corresponding console output (=pseudo debugging).

Also see documentation at

and related questions:

Upvotes: 6

User33
User33

Reputation: 294

The debug package is probably what you want. If you are debugging via this package an extra window opens in which your code is displayed and then you can debug line by line in combination with RStudio.

EDIT:

See below example code for how to debug with the debug package:

install.packages("debug")
library(debug)

fun <- function(x) {
  y <- x + 1
  z <- y + 1
  return(z)
}

mtrace(fun)
fun(2)

Upvotes: 2

Related Questions