在 R 中调试的一般建议 [英] General suggestions for debugging in R

查看:30
本文介绍了在 R 中调试的一般建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我编写的 R 函数时出错:

I get an error when using an R function that I wrote:

Warning messages:
1: glm.fit: algorithm did not converge 
2: glm.fit: algorithm did not converge 

我做了什么:

  1. 单步执行函数
  2. 添加打印以找出错误发生在哪一行表明不应使用 glm.fit 的两个函数.它们是 window()save().
  1. Step through the function
  2. Adding print to find out at what line the error occurs suggests two functions that should not use glm.fit. They are window() and save().

我的一般方法包括添加 printstop 命令,并逐行单步执行函数,直到找到异常为止.

My general approaches include adding print and stop commands, and stepping through a function line by line until I can locate the exception.

但是,我不清楚使用这些技术时代码中出现此错误的地方.我什至不确定代码中的哪些函数依赖于 glm.fit.我该如何诊断这个问题?

However, it is not clear to me using those techniques where this error comes from in the code. I am not even certain which functions within the code depend on glm.fit. How do I go about diagnosing this problem?

推荐答案

我想说调试是一种艺术形式,所以没有明确的灵丹妙药.任何语言都有很好的调试策略,它们也适用于这里(例如 阅读这篇不错的文章).例如,第一件事是重现问题...如果您不能这样做,那么您需要获取更多信息(例如使用日志记录).一旦你可以重现它,你需要减少到源头.

I'd say that debugging is an art form, so there's no clear silver bullet. There are good strategies for debugging in any language, and they apply here too (e.g. read this nice article). For instance, the first thing is to reproduce the problem...if you can't do that, then you need to get more information (e.g. with logging). Once you can reproduce it, you need to reduce it down to the source.

与其说是技巧",不如说我有一个最喜欢的调试例程:

Rather than a "trick", I would say that I have a favorite debugging routine:

  1. 发生错误时,我通常做的第一件事是通过调用 traceback() 查看堆栈跟踪:它会显示错误发生的位置,如果您有多个嵌套函数.
  2. 接下来我将设置options(error=recover);这会立即切换到发生错误的浏览器模式,因此您可以从那里浏览工作区.
  3. 如果我仍然没有足够的信息,我通常使用 debug() 函数并逐行逐步执行脚本.
  1. When an error occurs, the first thing that I usually do is look at the stack trace by calling traceback(): that shows you where the error occurred, which is especially useful if you have several nested functions.
  2. Next I will set options(error=recover); this immediately switches into browser mode where the error occurs, so you can browse the workspace from there.
  3. If I still don't have enough information, I usually use the debug() function and step through the script line by line.

R 2.10 中最好的新技巧(使用脚本文件时)是使用 findLineNum()setBreakpoint() 函数.

The best new trick in R 2.10 (when working with script files) is to use the findLineNum() and setBreakpoint() functions.

作为最后的评论:根据错误,在外部函数调用周围设置 try()tryCatch() 语句也非常有帮助(尤其是在处理与 S4 类).这有时会提供更多信息,并且还可以让您更好地控制在运行时如何处理错误.

As a final comment: depending upon the error, it is also very helpful to set try() or tryCatch() statements around external function calls (especially when dealing with S4 classes). That will sometimes provide even more information, and it also gives you more control over how errors are handled at run time.

这些相关问题有很多建议:

These related questions have a lot of suggestions:

这篇关于在 R 中调试的一般建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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