How to step through an R script from the beginning?
I want to step through an R script. I saw the "debug" command while searching for how to do this but th开发者_JAVA百科at 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?
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:
- Create a new R script (
ctrl+shift+n
) - Enter code in the file
- 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 abreak point
- a) clicking left of the code line number where you want to set a
- Save the file
- Enter debugging mode and source the file by
- a) checking the
Source on Save
box (above theSource
window) and then saving the file, - b) clicking the
Source
button at the top-right of theSource
window, - c) entering
debugSource("<yourfilename>")
+enter
in theConsole
, or - d) entering
ctrl+shift+s
- a) checking the
- Go through the debugging process
For more steps on debugging in RStudio, see this help file (dated April 23, 2015 12:59).
I'm partial to RStudio, so I recommend the following:
- Download RStudio
- Open your R Script
- put your cursor on the first line
- click ctrl + enter (PC/Linux) or command + return (Mac)
- repeat!
A nice feature of RStudio is that RStudio server can run great on a headless server. You then connect to the server via http in a web browser on your local machine. I use this configuration when running R on EC2 instances. RStudio maintains state so if I lose internet access on my train ride, when I get signal back RStudio picks up exactly where I left off and my remote machine has no idea that I disconnected and reconnected. Note that RStudio server is currently only supported on FC/CentOS and Debian/Ubuntu. Although it may compile under other variants of *nix.
One popular way is to do this from your 'IDE' or editor -- Emacs / ESS do it very well, others do it too.
The basic idea is that you send either the line under the cursors, or function, or block, ... to the associated R process. Several other editors support this, including RStudio. My preference is still with ESS, but I am sure you can find something suitable.
Lastly, browser()
et al can do that from within the R process but it is a little less pointy-clickety. Read the documentation, or books like Chambers "Software for Data Analysis" (Springer, 2008).
For an R only solution, which evaluates complete expressions rather than individual lines, try this:
sourcep <- function(file){
coms <- parse(file)
for (i in seq_along(coms)){
print(coms[[i]])
eval(coms[[i]],envir=.GlobalEnv)
mess <- paste("Expression",i,"of",length(coms),"parsed. Press <return> to continue.")
cat(mess)
readLines(n=1)
}
}
You call this as you would call source
(though this is much more basic and doesn't include any of its options). Basically, it uses parse
to create a list of the parsed, but unevaluated expressions from your source file, then iterates through this list to print the expression, evaluate it in the global environment and then put a message to indicate the progess. The final line is the one that creates the pausing: a call to read a single line from stdin()
.
You mention in a comment that you run your code with Rscript, but to debug you need to run it interactively. In other words, to get browser
to "work", start up a regular R console and source
your script. Since the first line of the script is browser()
, you'll immediately get back to the prompt, at which you can use "n" to single step through the code. But, since it's a full-blown R prompt, you can also check or change any variable value just by typing its name or assigning to it. (Or indeed, invoke arbitrary R functions.)
As suggested in another answer, inserting browser()
at the beginning of your script, getting an interactive session by running R
rather than Rscript
, and then using source('myscript.Rscript')
from R
will do the trick if your script does not require commadline arguments (e.g. like those processed by argparse).
If you do need to run with commandline args, you can simply invoke R
with the --args
options (e.g. R --no-restore --no-save --args <your commandline args here>
) and the sourced script will see the arguments as if it had been invoked using Rscript. There are other solutions to the question of how to pass commandline args to source
, but they don't target this specific use case where source
will be called only once.
精彩评论