How can I debug efficiently in R if packages are unknown due to lazy evaluation. I would like to keep the basic browser()
functionality as it works great - even
The following works for me.
I have my package TestDebug
with my function
myfun <- function(a,b) {return(a+b)}
If I run the script
debug(TestDebug::myfun)
TestDebug::myfun(1,2)
The debugger steps right into the source of TestDebug::myfun()
not into the ::
function as it does when you place a browser()
before the call to TestDebug::myfun(1,2)
.
As you mention in your question: in real-life situations TestDebug::myfun(1,2)
often calls otherpackage::myfun2(1,2)
. If you try to step into otherpackage::myfun2(1,2)
you will end up inside the ::
function again.
To prevent this I add this functions called inside other functions to the debug
index on the fly:
As soon as you are on the line inside TestDebug::myfun()
where otherpackage::myfun2(1,2)
is called I run debug(otherpackage::myfun2(1,2))
in the console. After that I can step into otherpackage::myfun2(1,2)
without problems and end up in the source code of otherpackage::myfun2(1,2)
. (..and not in the source code of ::
)
Don't forget to call undebug(otherpackage::myfun2(1,2))
after you're sure that your problem is not inside otherpackage::myfun2(1,2)
to prevent the debugger to jump into otherpackage::myfun2(1,2)
the next time it is called.
If you prefer you can also use debugonce(otherpackage::myfun(1,2))
(instead of debug(..)
) to only debug a function once.