There's a definitive guide for how to view the source code for a function, but how do you debug a function that's not exported from a package, without manually stepping through the source code that you've found?
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"
It's not at all obvious, but giving the argument as a symbol rather than as a quoted string seems to work fine, i.e.
debugonce(plm:::predict.plm)
rather than
debugonce("plm:::predict.plm")
One trick I've used is to assign first to a local object:
predict.plm <- plm:::predict.plm
after which you can do fix()
, debug()
, ... the local copy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With