Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to step through an R script from the beginning?

Tags:

r

debugging

I want to step through an R script. I saw the "debug" command while searching for how to do this but that seems to only apply to functions. This script doesn't have any functions.

The "browser" command looked promising so I put "browser()" as the first line of my script but it didn't seem to do anything when I ran it.

How do I get the script to pause on the first line so I can step through it?

like image 572
Greg Avatar asked Sep 20 '11 13:09

Greg


People also ask

How do you enter debug mode in R?

In addition to debug() and browser(), you can also enter debug mode by setting “editor breakpoints” in RStudio by clicking to the left of the line in RStudio, or by selecting the line and typing shift+F9.

How do I run part of an R script?

Ctrl + Enter – Runs the current line and jumps to the next one, or runs the selected part without jumping further. Alt + Enter – Allows running code without moving the cursor to the next line if you want to run one line of code multiple times without selecting it. Ctrl + Alt + R – Runs the entire script.


1 Answers

I was racking my brain trying to figure this out (stepping through a script without a specific function to call) in RStudio's IDE Version 0.98.1102.

Solution for a new script in RStudio:

  1. Create a new R script (ctrl+shift+n)
  2. Enter code in the file
  3. Set a break point by
    • a) clicking left of the code line number where you want to set a break point (red dot) or
    • b) adding browser() to the code line where you want to set a break point
  4. Save the file
  5. Enter debugging mode and source the file by
    • a) checking the Source on Save box (above the Source window) and then saving the file,
    • b) clicking the Source button at the top-right of the Source window,
    • c) entering debugSource("<yourfilename>") + enter in the Console, or
    • d) entering ctrl+shift+s
  6. Go through the debugging process

For more steps on debugging in RStudio, see this help file (dated April 23, 2015 12:59).

like image 161
Daniel Fletcher Avatar answered Sep 27 '22 20:09

Daniel Fletcher