R script line numbers at error? [duplicate]

牧云@^-^@ 提交于 2020-01-29 07:55:47

问题


I found this post from a year ago, and I'm using R version 2.11.1 (2010-05-31), but still getting error messages without line numbers.

Any solution?


回答1:


The answers given there are still valid. Returning line numbers from a script ain't that straight-forward, but R can give you a lot more information on where the error can be found.

You could use the error options to save the info in a file, for example :

options(error = quote({
  sink(file="error.txt");
  dump.frames();
  print(attr(last.dump,"error.message"));
  traceback();
  sink();
  q()}))

The function findLineNum() could be used if you have the name of the file somewhere available. If you have the error message, you could do something like :

dump.frames()
x <- attr(last.dump,"error.message")
ll <- gsub("Error in (.*) : .*","\\1",x)
lln <- findLineNum(srcfile,ll)



回答2:


In the upcoming R 2.14, the core team is making progress toward implementing this feature. Functions in scripts loaded with source(file=..., keep.file=TRUE) will contain an attribute srcref, which identifies the range of characters corresponding to the function's definition in an in-memory copy of the source file stored as an object of class srcfilecopy.

This does not immediately provide line-level debugging, but it lets you get approximate line numbers if you're willing to get your hands dirty. Also, it's progress.



来源:https://stackoverflow.com/questions/3650444/r-script-line-numbers-at-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!