0

If you have a block of R code, a combination of commands and functions, and you want to debug the code, line by line, and kinda go through the internal process R is doing behind the scene before it spits out an error message, what commands could one employ? trace() and debug() seem to be just for functions. I am trying to run my entire script and find out, line item by line item, what R is doing internally along each commend line.

I researched past archives and I found this: Debug Tools in R - stepping through code but alas with no solution to the problem

Community
  • 1
  • 1
jessica
  • 1,325
  • 2
  • 21
  • 35
  • 1
    I'm no expert but [RStudio](http://www.rstudio.com/ide/docs/debugging/overview) might be something for you. – Frithjof Dec 14 '13 at 21:30
  • Hi Frith. I am using R studio. It still requires debugging. – jessica Dec 14 '13 at 21:31
  • What exactly do you want to happen? Running the code line-by-line (no need for debug unless you're looking inside functions) seems like it would give what you want and then use `traceback` to figure out where errors come from. – Thomas Dec 14 '13 at 21:33
  • Correct me if I am wrong, `traceback` is only for debugging functions that call on other functions. It outputs the sequence of functions in the order they are called. – jessica Dec 14 '13 at 21:44

3 Answers3

1

Try the browser function. Press 'n' to go step by step.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
1

I would encourage doing this in an integrated development environment (IDE) rather than doing this in the code. Using an IDE keeps your code clean and simple.

I use an IDE called "RStudio" (http://www.rstudio.com), which is available for windows, mac, and linux and is pretty easy to use.

Newish version of Rstudio have the capability to add breakpoints in scripts and functions: to do this, just click on the left margin of the file to add a breakpoint. You can set a breakpoint and then step through from that point on. You also have access to all of the data in that environment, so you can try out commands.

See http://www.rstudio.com/ide/docs/debugging/overview for details. If you already have Rstudio installed, you may need to upgrade - this is a relatively new (late 2013) feature.

You may also find other IDEs that have similar functionality.

Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
0

In Rstudio, pressing Ctrl+R without any text highlighted will run whatever code is on the same line as your cursor. Then your cursor will automatically advance to the next line. You can just keep pressing Ctrl+R to run through your code line by line.

Not sure what else you want. Adding ; print(whatever) at the end of each line is always an option too, but it gets tedious.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96