traceback()对于在source()期间发生的错误返回无用的信息 [英] traceback() returns useless information for error happened during source()

查看:77
本文介绍了traceback()对于在source()期间发生的错误返回无用的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找文件源时很难在R中准确找到错误发生的位置,这让我感到惊讶.例如:

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

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

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")

这是无用的信息.

我做错什么了吗?

我正在寻找一种解决方案,该解决方案将产生一条消息,Python将产生此消息:

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

推荐答案

设置 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

这篇关于traceback()对于在source()期间发生的错误返回无用的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆