5

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

Lucky
  • 347
  • 6
  • 15
  • Doesn't the Next option do what you are expecting, i.,e step through the code? If not, what exactly do you mean by line by line debugging? – shark1608 Mar 31 '15 at 00:56
  • @shark1608 This actually works but I was looking for something like "debug" option. – Lucky Mar 31 '15 at 15:56

2 Answers2

6

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:

Stefan
  • 10,010
  • 7
  • 61
  • 117
2

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)
User33
  • 294
  • 3
  • 10
  • Yes, debug is what I wanted. I went through the tutorial. Thank you :-) But, when I install the debug package, its actually not prompting any window to debug. Is there something wrong with my machine or software ? – Lucky Mar 31 '15 at 15:57
  • 1
    I added a small example to my earlier post. Running the above code should generate an extra window. – User33 Apr 01 '15 at 07:46
  • 1
    @User33 Why I cannot install this package? Warning in install.packages : package ‘debug’ is not available for this version of R –  Nov 29 '20 at 01:00
  • @Hermi I think the package is no longer maintained. Debugging in R Studio is now further developed than at the time of my answer (5 years ago). Hence, using the debugging tools that are described in the answer by Stefan and are available in R Studio https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio seems to be a good alternative. – User33 Dec 01 '20 at 19:19