[R] Errors and line numbers in scripts?
Duncan Murdoch
murdoch.duncan at gmail.com
Thu May 12 17:21:43 CEST 2011
On 12/05/2011 11:02 AM, Elliot Joel Bernstein wrote:
> Is it possible to get R to report the line number of an error when a script
> is called with source()? I found the following post from 2009, but it's not
> clear to me if this ever made it into the release version:
It does so for parse errors. It doesn't record lines of statements at
the top level of a script for run-time errors, but if you define a
function in your script, and call it at a different line, and an error
occurs internally, then R will know which line of the function triggered
the error. If you use options(error=recover) you'll get a display
something like this:
> source("c:/temp/test.R")
Error in f(0) : an error in f
Enter a frame number, or 0 to exit
1: source("c:/temp/test.R")
2: eval.with.vis(ei, envir)
3: eval.with.vis(expr, envir, enclos)
4: g()
5: test.R#6: f(0)
The call to g() was in the test.R script but was not recorded. However,
the function g contains a call to f(0), and that one was recorded as
being at line 6 of the file.
Duncan Murdoch
Here's the test.R file I used:
--------------------------
f <- function(x) {
stop("an error in f")
}
g <- function() {
f(0)
}
g()
----------------------------
> ws wrote:
> >* Is there a way to have R return the line number in a script when it errors out?
> *>*
> *>* I call my script like:
> *>*
> *>* $ R --vanilla< script.R> output.txt
> *>*
> *>* I seem to remember a long discussion about this at some point, but I can't
> *>* remember the outcome.
> *>*
> *>*
> *
> The current development version returns much more information about
> error locations. I don't know if it will handle this case (R doesn't
> get told the filename in case of input redirection, for example).
> Generally the goal is to report error lines during interactive use,
> batch use is assumed to already be debugged.
>
> Duncan Murdoch
>
>
> Thanks.
>
> - Elliot
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list