Skip to main content

Debug with debugr

In this tutorial, you will learn about debugging in R and Rstudio using the debugr package.
Sep 2018  · 6 min read

When we talk about debugging in R, there is a slight negative aspect in people who work on python and other programming IDEs. There are some useful functions in R itself like traceback() and browser(), and interactive tools in RStudio but somehow there are few limitations which are not handled by built-in functionalities. In such scenarios, we get stuck with our issues, and that takes much more time to resolve.

Then arrives the debugr package which you can use while writing complex methods and loops which shows you the runtime variable entries and assists in solving the bug in much less time. Although you require preplanning for code entries and the points on an action for debugr. It's worth to put some time in it than resolving the issue the hard way at the time of its execution.

Aspects of Debugging in Rstudio

Rstudio provides an interactive platform for debugging that makes detecting bugs in complex functions much easier.

Let's draw a function below and apply these methods.

f <- function(a) g(a) g <- function(b) h(b) h <- function(c) i(c) i <- function(d) j(d) j <- function(e) “a” + e

f(10)

rstudio

Here you can browse to different functions to check the code in an iterative way. The function you browse will show its code.

functions
traceback flow

You can use traceback to see the flow of function calls made by individual ones.

Debugging on Error

While executing code in Rstudio, if an error comes then Rstudio provides two methods in its interactive console to resolve it. One is traceback that shows you the flow of the function calls, and the other is Rerun with debug. If you choose rerun with debug, Rstudio will take you to the interactive debugger session which also pauses the execution where the error occurred.

When you are in the editor you can use it either with the console or with the keyboard:

  • next button, n: This button will take you to the next step.

  • s button, or s: works like next, but instead of next step it steps into next function.

  • f button, or f: finishes execution of the current loop or function.

  • c button, c: leaves interactive debugging.

  • q button, Q: stops debugging, terminates the function, and returns to the global workspace.

Setting Breakpoints in arbitrary code

Rstudio enters in the interactive console on an error, but you can enter at an arbitrary code location by using either a Rstudio breakpoint or browser().

The breakpoint can be added through several methods:

  1. You can click on the left of the line of the code.
  2. You can press Shift+F9 on the line of the code.
  3. You can add browser() where the execution needs to be stopped.

RStudio currently does not support conditional breakpoints that put a breakpoint by passing criteria, which is somewhat handled in the debugr package discussed in this tutorial. *

For details on these debugging methods visit here.

Now let's talk about the package debugr.

Debugr supports a simpler aspect of debugging which is easy to understand if you are involved in less debugging work. Debugr has a few simple functions, they are:

  • debugr_isActive:- Switching debug mode on and off.
  • debugr_switchOff:- Switching debug mode on and off.
  • debugr_switchOn:- Switching debug mode on and off.
  • dwatch:- Printing debug outputs during runtime.

Out of all these functions, dwatch plays the most crucial role. It prints a debug output to a console or a file in a more complicated case. The output can be static text, one or many values of the variables.

For any debugging function, you need switch on the debugging functionality of debugr which is easily done by debug_switchOn(). You can put your dwatch() function anywhere you want to debug the code especially in production level code and debug it whenever a situation came by switching it on and later switch it off by using debugr_switchOff() function.

Let's look at the structure of the dwatch function in a sample code.

 library(debugr)

debugr_switchOn()

myfunction <- function(x) {

  z <- 1

  for(i in 1:x) {
    dwatch(crit = "z > 40000", objs = c("z", "i"))
    z <- z * i
  }
  # avoiding to print the z
  invisible(z)
}

myfunction(10)
##
## ----------------------------- DEBUGR MESSAGE ------------------------------
##
## ** z:
## [1] 40320
##
##
## ** i:
## [1] 9
##
## ---------------------------------------------------------------------------
##
## ----------------------------- DEBUGR MESSAGE ------------------------------
##
## ** z:
## [1] 362880
##
##
## ** i:
## [1] 10
##
## ---------------------------------------------------------------------------
debugr_switchOff()

Here the code is going through iterative multiplication, or you can apply any case used in your functions, the dwatch function applied to the variable z with the criteria z > 4000. So as a result messages are printed when the criteria is fulfilled and the desired results got printed that is values of z and i variables.

You can use some sophisticated arguments that manipulate the format of the output.

 debugr_switchOn()

myfunction <- function(x) {

  z <- 1

  for(i in 1:x) {
    dwatch(crit = "z > 40000", expr=c("format(z, big.mark = \",\")", "format(i, big.mark = \",\")"))
    z <- z * i
  }
  # avoiding to print the z
  invisible(z)
}

myfunction(10)
##
## ----------------------------- DEBUGR MESSAGE ------------------------------
##
## ** Expression: format(z, big.mark = ",")
## [1] "40,320"
##
## ** Expression: format(i, big.mark = ",")
## [1] "9"
## ---------------------------------------------------------------------------
##
## ----------------------------- DEBUGR MESSAGE ------------------------------
##
## ** Expression: format(z, big.mark = ",")
## [1] "362,880"
##
## ** Expression: format(i, big.mark = ",")
## [1] "10"
## ---------------------------------------------------------------------------
debugr_switchOff()

There are few more arguments that you can incorporate:

  • show.all: By setting this argument TRUE, you can see all the objects for the criteria.
  • msg: It is used to add a static text message.
  • show.frame: By setting this argument as FALSE, you can remove upper and lower border.
  • halt: By setting this argument as TRUE, you can stop the transaction as soon as the criteria is fulfilled.

If you would like to learn more about debugging in R, take DataCamp's Working with the RStudio IDE (Part 1) course.

Introduction to R

Beginner
4 hours
2,395,721
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
See DetailsRight Arrow
Start Course

Intermediate R

Beginner
6 hours
532,111
Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.

Introduction to the Tidyverse

Beginner
4 hours
261,843
Get started on the path to exploring and visualizing your own data with the tidyverse, a powerful and popular collection of data science tools within R.
See all coursesRight Arrow
Related
Data Science Concept Vector Image

How to Become a Data Scientist in 8 Steps

Find out everything you need to know about becoming a data scientist, and find out whether it’s the right career for you!
Jose Jorge Rodriguez Salgado's photo

Jose Jorge Rodriguez Salgado

12 min

Predicting FIFA World Cup Qatar 2022 Winners

Learn to use Elo ratings to quantify national soccer team performance, and see how the model can be used to predict the winner of FIFA World Cup Qatar 2022.

Arne Warnke

DC Data in Soccer Infographic.png

How Data Science is Changing Soccer

With the Fifa 2022 World Cup upon us, learn about the most widely used data science use-cases in soccer.
Richie Cotton's photo

Richie Cotton

Regular Expressions Cheat Sheet

Regular expressions (regex or regexp) are a pattern of characters that describe an amount of text. Regular expressions are one of the most widely used tools in natural language processing and allow you to supercharge common text data manipulation tasks. Use this cheat sheet as a handy reminder when working with regular expressions.
DataCamp Team's photo

DataCamp Team

ggplot2 Cheat Sheet

ggplot2 is considered to be one of the most robust data visualization packages in any programming language. Use this cheat sheet to guide your ggplot2 learning journey.
DataCamp Team's photo

DataCamp Team

A Guide to R Regular Expressions

Explore regular expressions in R, why they're important, the tools and functions to work with them, common regex patterns, and how to use them.
Elena Kosourova 's photo

Elena Kosourova

16 min

See MoreSee More