Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a non-exported function?

Tags:

r

debugging

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"
like image 738
Ari B. Friedman Avatar asked Mar 02 '14 20:03

Ari B. Friedman


2 Answers

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") 
like image 56
Ben Bolker Avatar answered Sep 28 '22 09:09

Ben Bolker


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.

like image 40
Dirk Eddelbuettel Avatar answered Sep 28 '22 08:09

Dirk Eddelbuettel