How to debug a code in R

This recipe helps you debug a code in R

Recipe Objective

How to debug code in R

While writing a piece of code, no matter how big or small, bugs and errors are likely to happen. These errors can be found out and corrected by three methods: traceback(), debug(), browser() The debug () function in R returns the execution of a code line by line, i.e in order to find out which line of code has caused an error, the debug () function can be used. When the debug () is executed, it enters the debug mode. In the debug mode, the line of code that is suspected to be a cause of error is highlighted. This helps understand exactly which line of code contains an error, and corrections can be made. This recipe demonstrates an example of debugging a code in R.

Step 1 - Define a function

new_func <- function(x,y){ add <- x + y multiply <- add * 3 total <- sum(multi) total }

Step 2 - Generate some random data

set.seed(1) x <- rnorm(50)

Step 3 - Try using the traceback()

new_func(10,x) # traceback() returns this as the error, we cannot exactly find out which line of code has generated the output traceback()

"Output of traceback()" :
Error in new_func(10, x): object 'multi' not found
Traceback:

1. new_func(10, x)

Step 4 -Use the debug()

debug(new_func) d <- new_func(10,x)

"Output of debug()" :
debugging in: new_func(10, x)
debug at #1: {
    add <- x + y
    multiply <- add * 3
    total <- sum(multi)
    total
}
Browse[2]> 
debug at #2: add <- x + y
Browse[2]> 
debug at #3: multiply <- add * 3
Browse[2]> 
debug at #4: total <- sum(multi)
Browse[2]> 
Error in new_func(10, x) : object 'multi' not found

Step 5 - Use the undebug()

undebug(new_func) # brings user out of the debugging mode. new_func(50,x) # returns back to undebug mode {"mode":"full","isActive":false}

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud