调试包:: function()虽然使用了延迟评估

时间:2016-05-24 15:06:21

标签: r rstudio

如果由于延迟评估而导致包未知,我如何在R中有效调试。我希望保留基本的browser()功能,因为它很有效 - 即使使用testthat package。如以下post中所述,在“项目选项=>构建工具”中为我的项目设置了--with-keep.source

要重现该行为,请创建包含

的包TestDebug
myfun <- function(a,b) {return(a+b)}

和脚本example.R

{
browser()
TestDebug::myfun(1,2)
}

修改:还应涵盖TestDebug::myfun(1,2)调用otherpackage::myfun2(1,2)的情况。我认为情况应该发生在每个“真实”的包裹中?

1 个答案:

答案 0 :(得分:5)

以下适用于我。

我的包TestDebug包含我的功能

myfun <- function(a,b) {return(a+b)}

如果我运行脚本

debug(TestDebug::myfun)
TestDebug::myfun(1,2)

调试器直接进入TestDebug::myfun()的来源,而不是::函数,就像在调用browser()之前放置TestDebug::myfun(1,2)一样。

正如您在问题中提到的那样:在现实生活中,TestDebug::myfun(1,2)经常会调用otherpackage::myfun2(1,2)。如果您尝试进入otherpackage::myfun2(1,2),您将再次进入::功能。

为了防止这种情况,我将其他函数内部调用的函数动态添加到debug索引中:

一旦你在调用TestDebug::myfun()的{​​{1}}内,我就在控制台中运行otherpackage::myfun2(1,2)。之后,我可以毫无问题地进入debug(otherpackage::myfun2(1,2))并最终进入otherpackage::myfun2(1,2)的源代码。 (..而不是otherpackage::myfun2(1,2))的源代码

在您确定问题不在::内之后,请不要忘记致电undebug(otherpackage::myfun2(1,2)),以防止调试程序在下次调用时跳转到otherpackage::myfun2(1,2)。< / p>

如果您愿意,也可以使用otherpackage::myfun2(1,2)(而非debugonce(otherpackage::myfun(1,2)))仅调试一次函数。

相关问题