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

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

问题描述

如果我从命令行运行一个长R脚本(R --slave script.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. debug()

  2. browser()

  3. options(error = recover)[后跟options(error = NULL)还原]

你可能想看看这个相关的帖子。

对不起...刚刚看到你是从命令行运行的。在这种情况下,我建议使用选项(错误)功能。这是一个简单的例子:

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天全站免登陆