在R中是否有任何功能检查一行代码是否返回错误? [英] Is there any function in R to check whether a line of code returns error or not?

查看:193
本文介绍了在R中是否有任何功能检查一行代码是否返回错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一行代码产生错误, R 中是否有任何函数返回 false

解决方案

如果您的意思是shell开发人员熟悉的内容,您可以在其中运行任意命令,然后强>检查成功或失败,然后答案是



在R中,所有错误都是致命的 - 他们立即终止执行程序。没有办法检查以后是否有事情发生,因为以后从未达到。



你可以做的是将某些应用程序包装到特殊块中暂时中止错误的严重性并允许对他们做出回应。这可以使用 尝试 tryCatch ,其中第一个包装在第二个(所以他们基本上做同样的事情)。



请注意,您无法以这种方式从语法错误中恢复 - 在评估任何代码之前,这些将被解析器捕获。



总的来说,我会推荐阅读调试,条件处理和防御性编程一章在高级R 书。如果遇到特定问题,请随时问更多问题。


Is there any function in R that returns false if a line of code produces error?

解决方案

If you mean something that would be familiar to shell developers, where you can run arbitrary command and then inspect whether it succeeded or failed, then the answer is no.

In R, all errors are fatal - they immediately terminate execution of program. There is no way to check if something has failed afterwards, because "afterwards" is never reached.

What you can do is wrap some part of application into special block that will temporarily suspend severity of errors and allow to respond to them. This can be done using try or tryCatch, where first one is wrapper around second (so they basically do the same thing).

Please note that you can't recover from syntax errors this way - these will be caught by parser before any code is evaluated.

Overall, I would recommend reading "Debugging, condition handling, and defensive programming" chapter in Advanced R book. Feel free to ask further questions if you encounter specific problems.

这篇关于在R中是否有任何功能检查一行代码是否返回错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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