0

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?

ivo Welch
  • 2,427
  • 2
  • 23
  • 31
  • 1
    How exactly are you running your scripts? There are many cases where the built in parser can track line numbers, but there are also many cases where that is turned off by default. – MrFlick Aug 12 '20 at 18:39
  • And you can certainly parse a file with the `parse()` command and then walk the abstract syntax tree to insert messages where you like. You just need to define exactly where you want to insert them. – MrFlick Aug 12 '20 at 18:42
  • 1
    ivo Welch, is it possible to come up with a simple example, either a `function` or a `file.R` script that triggers this behavior? For instance, if I naively write `file.R` with simply `myfunc <- function() 1+"a"` as its contents, then `source("file.R")`, when I call `myfunc()`, it tells me `Error in 1 + "a" (from file.R#1) : ...`, which seems to do what you intend. – r2evans Aug 12 '20 at 18:48
  • thx everyone. I have now provided an example that can be stuck into a file and run via `source()`. r2e: apparently, some give error numbers, some do not. MrFlick: is there a way to turn all the line numbers back on? – ivo Welch Aug 13 '20 at 20:01

0 Answers0