如何调试非导出函数?

时间:2014-03-02 20:42:29

标签: r debugging

sa definitive guide有关如何查看函数的源代码,但是如何调试未从包中导出的函数,而无需手动单步执行源代码你找到了吗?

library(plm)
> predict.plm
Error: object 'predict.plm' not found
> plm:::predict.plm
function (object, newdata = NULL, ...) 
{
    tt <- terms(object)
    if (is.null(newdata)) {
        result <- fitted(object, ...)
    }
    else {
        Terms <- delete.response(tt)
        m <- model.frame(Terms, newdata)
        X <- model.matrix(Terms, m)
        beta <- coef(object)
        result <- as.numeric(crossprod(beta, t(X)))
    }
    result
}
<environment: namespace:plm>
> debugonce("predict.plm")
Error in debugonce("predict.plm") : could not find function "predict.plm"
> debugonce("plm:::predict.plm")
Error in debugonce("plm:::predict.plm") : 
  could not find function "plm:::predict.plm"

3 个答案:

答案 0 :(得分:12)

这一点都不明显,但是将参数作为符号而不是引用的字符串似乎工作正常,即

debugonce(plm:::predict.plm)

而不是

debugonce("plm:::predict.plm")

答案 1 :(得分:7)

我使用的一个技巧是首先分配给本地对象:

predict.plm <- plm:::predict.plm

之后,您可以执行fix()debug(),......本地副本。

答案 2 :(得分:0)

我这样做:假设您必须调试代码行

rmarkdown::render("myDocument.Rmd") 

然后你可以在代码行下面添加avobe y

f <- function () {
  rmarkdown::render("myDocument.Rmd") 
}

下一步

debug(f)
f ()