[R] line numbers and file names in error messages
Jan T. Kim
jtk at cmp.uea.ac.uk
Fri Apr 15 21:19:15 CEST 2005
On Fri, Apr 15, 2005 at 12:37:46PM -0400, Gabor Grothendieck wrote:
> On 4/15/05, Jan T. Kim <jtk at cmp.uea.ac.uk> wrote:
> > On Fri, Apr 15, 2005 at 11:27:13AM -0400, Gabor Grothendieck wrote:
> > > On 4/15/05, Vivek Rao <rvivekrao at yahoo.com> wrote:
> > > > Many of my R scripts call other R scripts using the
> > > > source function. If there is a syntax error in one of
> > > > the scripts, I get an error message such as
> > > >
> > > > Error in parse(file, n, text, prompt) : syntax error
> > > > on line 1
> > > >
> > > > but the name of the file where the error occurs is not
> > > > given. Other error messages such as
> > > >
> > > > Error in print(xxx) : Object "xxx" not found
> > > >
> > > > show neither the file name or the line number. Is
> > > > there a way to get this information in error messages?
> > > > I have found it helpful in other programming
> > > > languages.
> > >
> > > Perhaps at the end of each script you could add a print statement
> > > to tell you it had successfully finished.
> >
> > No, this won't help. The trouble is that the first type of error is
> > detected during parsing while the second type of error occurs during
> > execution. For the parser, the line
> >
> > print(xxx);
> >
> > is perfectly fine, the error is that the thing to be printed does
> > not exist. At the time of execution, the information about which
> > line in what file contained the code that caused the problem.
>
> Actually it does help. The last line of the file will only be reached
> if there are no errors that prevent it from reaching there regardless
> of their type. Thus if the print executed you know that that sourced file
> finished allowing you to determine which ones worked.
Sorry if I sounded a bit harsh here. What I meant is that the printing
approach may be misleading. Consider:
# file1.r
foo <- function()
{
print(xxx);
}
print("file1: success");
# file2.r
source("file1.r");
bar <- function()
{
foo();
}
bar();
xxx <- "hello, world";
print(xxx);
print("file2: success");
> source("file2.r");
[1] "file1: success"
Error in print(xxx) : Object "xxx" not found
Now, the unsuspecting may easily be misled to believe that print(xxx) in
file2.r is at fault, whereas the traceback reveals that the foo function
is the culprit:
> traceback()
6: print(xxx)
5: foo()
4: bar()
3: eval.with.vis(expr, envir, enclos)
2: eval.with.vis(ei, envir)
1: source("file2.r")
Best regards, Jan
--
+- Jan T. Kim -------------------------------------------------------+
| *NEW* email: jtk at cmp.uea.ac.uk |
| *NEW* WWW: http://www.cmp.uea.ac.uk/people/jtk |
*-----=< hierarchical systems are for files, not for humans >=-----*
More information about the R-help
mailing list