Question

I am surprised by how hard it is to find where the error happened exactly in R when I am sourcing a file. For example:

> source('Data-Generation.R')
... # some output here
Error in as.matrix(X) %*% coefs[ix_X] : non-conformable arguments

At least in this case, I can find where it is by searching for it because I have as.matrix(X) %*% coefs[ix_X] only once in my code. Anyway, if I try to pinpoint where it happened with traceback():

> traceback()
4: eval(expr, envir, enclos)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("Data-Generation.R")

which is useless information.

Am I doing something wrong?

Edited: I am looking for a solution which will produce a message that Python would yield:

$ python .\test.py

1
Traceback (most recent call last):
  File ".\test.py", line 5, in <module>
    1/0
ZeroDivisionError: integer division or modulo by zero

No correct solution

OTHER TIPS

Set echo = TRUE:

source(textConnection("i <- 1
                      y*x
                      3+4"), echo=TRUE)
#> i <- 1
#
#>                       y*x
#Error in eval(expr, envir, enclos) : object 'y' not found

verbose = TRUE might also be useful:

source(textConnection("i <- 1
                      y*x
                      3+4"), verbose=TRUE)
#'envir' chosen:<environment: R_GlobalEnv>
#--> parsed 3 expressions; now eval(.)ing them:
#
#>>>> eval(expression_nr. 1 )
#        =================
#
#> i <- 1
#curr.fun: symbol <-
# .. after ‘expression(i <- 1)’
#
#>>>> eval(expression_nr. 2 )
#        =================
#
#>                       y*x
#Error in eval(expr, envir, enclos) : object 'y' not found
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top