Dive – the debugging function you deserve

[This article was first published on r-bloggers – STATWORX, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A programmer’s misery

Writing R functions is always fun up until the point where you have to debug them. Especially if you are layering functions. In the best case, you can just use the native debug functionalities of R. However, in my opinion this is only feasible when you code in RStudio, because you can use the interface to maneuver around. If you like to code in another editor, e.g. Sublime or Notepad++ or just don’t want to use the debug functionalities of R, then you are probably familiar with the following scenario:

gif with debugging before dive

Right, this is painful to watch. Executing line by line, trying not to execute the pesky comma? Ugh. Honestly, if you have two to three arguments it is not that bad, but having large functions with 20+ arguments this always becomes a programmer’s misery. By now you probably know where this is headed. Yupp, we are going to use a function to help us out of that misery. You can get it on the STATWORX as well as my own GitHub account.

Introducing dive

The function really is as simple as it gets. It has two arguments, a string and the other is a switch for the output format. The string is the function call you want to debug. So, after seeing the long and stony path of debugging without dive, here’s what you must do using the function:

gif with debugging after dive

Essentially, you are just pasting the whole junk into the function and as a result get the function’s arguments printed into the console. From there you can copy them conveniently into the editor of your choice. The only thing you must look out for are quotation marks in your function call. You have to escape all quotation marks in your function call before executing the dive call. If you are not a fan of all these copy and paste shenanigans, there is an option to return the function arguments as a list. The advantage of this option in my opinion is that you do not have to paste the whole stuff somewhere in your code, but you have one list with all arguments. Of course, you can adjust this list as you please, like you would change it in the console option. When all your options are correctly specified you can just use the function list2env(your_dive_list_return) to evaluate all function’s arguments. Since, sometimes this might not be desirable, for instance, if you are 100% sure you don’t need the arguments a second time, dive can evaluate the arguments directly into your environment by setting return = “env”.

But wait, there’s more…

If you are not sold by now, then I might catch you with the following feature. You can also use dive with any apply function. This feature was the main motivation for me to code a debug supporting function. In my projects, I try to circumvent using any kind of loops whenever it is feasible. Thus, I code many functions and use them with an apply function instead. However, this is very painful to debug with the native debug functionalities of R and RStudio. With dive you can stop at every function level and change the inputs conveniently. So, how can you use the function with applies? Well, just like you would use it without apply functions. Just paste the stuff into dive. Of course, you are limited with the output option when you want to debug apply functions, since the iterator cannot be evaluated in the environment. However, the output formats list and console are still valid.
If you have any ideas how to improve the concept or if you are missing some features, just drop me an e-mail.

Über den Autor
André Bleier

André Bleier

André ist im Data Science Team und organisiert intern unsere Afterwork Veranstaltungen. In seiner Freizeit treibt er Sport und programmiert gerne kleine und große R Codes.

Der Beitrag Dive – the debugging function you deserve erschien zuerst auf STATWORX.

To leave a comment for the author, please follow the link and comment on their blog: r-bloggers – STATWORX.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)