R 脚本行号出错? [英] R script line numbers at error?

查看:21
本文介绍了R 脚本行号出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从命令行 (R --slave script.R) 运行一个很长的 R 脚本,我怎样才能让它在出错时给出行号?

If I am running a long R script from the command line (R --slave script.R), how can I get it to give line numbers at errors?

如果可能的话,我不想向脚本添加调试命令——我只是希望 R 的行为像大多数其他脚本语言一样......

I don't want to add debug commands to the script if at all possible -- I just want R to behave like most other scripting languages ...

推荐答案

这不会给你行号,但它会告诉你调用堆栈中失败发生的位置,这非常有帮助:

This won't give you the line number, but it will tell you where the failure happens in the call stack which is very helpful:

traceback()

从命令行运行脚本时,您必须跳过一两个调用,请参阅 traceback() 用于交互式和非交互式 R 会话

When running a script from the command line you will have to skip one or two calls, see traceback() for interactive and non-interactive R sessions

我不知道没有通常的调试嫌疑人的另一种方法:

I'm not aware of another way to do this without the usual debugging suspects:

  1. 调试()
  2. 浏览器()
  3. options(error=recover) [后面跟着 options(error = NULL) 来恢复它]

你可能想看看这个相关帖子.

[Edit:] 抱歉...刚刚看到您正在从命令行运行它.在这种情况下,我建议使用 options(error) 功能.这是一个简单的例子:

Sorry...just saw that you're running this from the command line. In that case I would suggest working with the options(error) functionality. Here's a simple example:

options(error = quote({dump.frames(to.file=TRUE); q()}))

您可以在错误情况下根据需要创建任意复杂的脚本,因此您应该决定调试所需的信息.

You can create as elaborate a script as you want on an error condition, so you should just decide what information you need for debugging.

否则,如果您关注特定领域(例如连接到数据库),请将它们包装在 tryCatch() 函数中.

Otherwise, if there are specific areas you're concerned about (e.g. connecting to a database), then wrap them in a tryCatch() function.

这篇关于R 脚本行号出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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