traceback()返回source()期间发生的错误的无用信息

时间:2014-03-03 16:07:58

标签: r debugging

当我在寻找文件时,我很难找到R中确切错误发生的地方。例如:

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

至少在这种情况下,我可以通过搜索来找到它的位置,因为我的代码中只有as.matrix(X) %*% coefs[ix_X]一次。无论如何,如果我试图用traceback()确定它发生的位置:

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

这是无用的信息。

我做错了吗?

已编辑:我正在寻找能够产生Python将产生的消息的解决方案:

$ 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

1 个答案:

答案 0 :(得分:2)

设置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也可能有用:

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