Take the 2-minute tour ×

If I am running a long R script from the command line (R --slave script.R), how can I get it to give line numbers at errors?

I don't want to add debug commands to the script if at all possible -- I just want R to behave like most other scripting languages ...

share|improve this question
Any updates? Four 4 years later, seems the problem still persists, despite all the mainstream adoption of R. –  Gui Ambros Sep 15 '13 at 0:41

3 Answers 3

up vote 15 down vote accepted

This won't give you the line number, but it will tell you where the failure happens in the call stack which is very helpful:


I'm not aware of another way to do this without the usual debugging suspects:

  1. debug()
  2. browser()
  3. options(error=recover) [followed by options(error = NULL) to revert it]

You might want to look at this related post.

[Edit:] Sorry...just saw that you're running this from the command line. In that case I would suggest working with the options(error) functionality. Here's a simple example:


You can create as elaborate a script as you want on an error condition, so you should just decide what information you need for debugging.

Otherwise, if there are specific areas you're concerned about (e.g. connecting to a database), then wrap them in a tryCatch() function.

share|improve this answer

Support for this will be forthcoming in R 2.10 and later. Duncan Murdoch just posted to r-devel on Sep 10 about findLineNum and setBreapoint:

I've just added a couple of functions to R-devel to help with
debugging.  findLineNum() finds which line of which function corresponds
to a particular line of source code; setBreakpoint() takes the output of
findLineNum, and calls trace() to set a breakpoint there.

These rely on having source reference debug information in the code.
This is the default for code read by source(), but not for packages.  To
get the source references in package code, set the environment variable
R_KEEP_PKG_SOURCE=yes, or within R, set options(keep.source.pkgs=TRUE),
then install the package from source code.  Read ?findLineNum for
details on how to
tell it to search within packages, rather than limiting the search to
the global environment.

For example,

x <- " f <- function(a, b) {
             if (a > b)  {
             } else {

eval(parse(text=x))  # Normally you'd use source() to read a file...

findLineNum("<text>#3")   # <text> is a dummy filename used by parse(text=)

This will print

 f step 2,3,2 in <environment: R_GlobalEnv>

and you can use


to set a breakpoint there.

There are still some limitations (and probably bugs) in the code; I'll
be fixing thos
share|improve this answer
Wow. Can't wait for R 2.10. –  Shane Sep 18 '09 at 20:37
There are daily r-devel source (and binaries for Windoze) ... –  Dirk Eddelbuettel Sep 18 '09 at 21:56
Thanks. Just signed up for the r-devel mailing list too. I've been avoiding r-help on the assumption that it would clog my inbox (r-sig-finance already does that). –  Shane Sep 19 '09 at 16:01

Doing options(error=traceback) appears quite useful: it causes a traceback to appear if there is an error, and the traceback has the line number, prefixed by #

share|improve this answer

Your Answer


