Debugging with print calls


#1

Hi everyone.

From times to times I read that debugging with print calls is evil.

I know about basic debugging functions like debug, debugonce, browser, traceback, trace, but in the end I'm often just more comfortable using print.

The problem I have with this strategy is that my code can end up cluttered with a mess of commented print calls that I have to hunt and remove when I'm done, and the one I forget to delete on time clutter the console too. I assume this is in good part why this approach is frown upon.

I've built this to get around it, which allows me to specify which category of debugging calls I want to execute (it doesn't have to be print):

options(dbg.ids = c("here","there")) # specify categories of calls

dbg <- function(id,expr){
  if(id %in% getOption("dbg.ids")){
    eval.parent(substitute(expr))
  }
}

dbg("here", print(1)) # [1] 1
dbg("foo", print(2))  # nothing happens

I'm thinking about coding some cleanup function that would use regex to remove all the calls from my project.

Is this a bad idea ? Are there some tools or habits that I should investigate before developing an exotic alternative ?


#2

I don't think that's true— I think it's the only option in some cases!

In June Amanda Gadrow did an RStudio webinar on debugging (video below):
https://www.rstudio.com/resources/videos/debugging-techniques-in-rstudio-amanda-gadrow-june-2018/

It sounds like you know the familiar tools, but she gets into some more advanced options for complex sources like packages and shiny apps that could be helpful.

The slides and code are here:


#3

Thank you Mara I will take a look