10 years later, I am still struggling with R script line numbers at error? . Errors like
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'print': argument is not interpretable as logical
leave me inserting by hand many message
statements to discover where it happened. (Even options(error=recover)
is not giving me anything more.)
I wonder if this could be done programmatically. That is, a parser for R syntax could know where it was allowed to insert a `message("[file:#]") at the line start, where '#' is the line number. (Of course, it cannot do so everywhere, because it could break code, e.g., in line continuations and non-{} statements) I would see a lot of messages flash by, but then know exactly where my error was and how it got there.
Let me give an example of what I mean. Say I have a program with a bug (and yes, I know how to fix bugs like this).
## much code, many other functions using kable, print etc.
library(knitr)
f <- function( d, ... ) {
## more code
print(kable( d, format="latex", ... ))
}
## ok mistake
d <- data.frame( x=rnorm(10), y=1:10 )
f(d)
## stupid mistake
f(d, format="latex")
What I really would like now is to xform my code into
## much code, many other functions using kable, print etc.
library(knitr)
message("[hooked.R:6]"); f <- function( d, ... ) {
## more code
message("[hooked.R:8]"); print(kable( d, format="latex", ... ))
}
## ok mistake
message("[hooked.R:12]"); d <- data.frame( x=rnorm(10), y=1:10 )
message("[hooked.R:13]"); f(d)
## stupid mistake
message("[hooked.R:14]"); f(d, format="latex")
message("[hooked.R:18]"); cat("bye")
The output now looks like
[hooked.R:14]
[hooked.R:8]
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'print': formal argument "format" matched by multiple actual arguments
And I know immediately where to start searching...line 8.
Does something like this exist?