Connect with us

Hi, what are you looking for?

Data Science

Debugging In R

Debugging in R
Debugging in R

In this first tutorial, we going to look at the debugging and debugging tools in r programming, also we’ll look at the mistake in programming. Let’s pass to the first subject.

Mistake Levels

There are different levels of errors. Some errors crash the program, while others provide information to improve the program. We have 4 bug types these types is message, warning, error, and condition.

Message = notification message produced by message() function.

Warning = notification message for wrong things produced by warning() function (not fatal)

Error = An indication that a fatal problem has occurred and the execution of the function stops this mistake produced by error().

Condition = uses for an unexpected event, the programmers can create their own conditions.

Mistake Level Examples

In this part, we going to create a simple function, we will test the functions we learned above in this application. If you are unfamiliar with functions and conditional structures, I suggest you read this article and this article.

echo <- function(x)
{
    if(x > 0) cat(log(x))
    if(x < 0) warning("x is NaN")
    
    invisible(x) # return x when function call
}

In this example, when logic (negative number), the value is Na. We used the warning function to notify the user.

loop <- function(x , y)
{
    if(y > x) stop("x is bigger than y")
    while(x > y) 
    {
        y <- y+1
        print(y)
    }
}

The function here creates a loop with the given values, but if the loop condition is not met, the function is terminated and the error message is written to the screen.

Debugging Tools

R offers a range of functions to facilitate your debugging and shorten your work. These functions are traceback, debug, and browser functions. summary and mean functions are also useful, you can also find information about these two functions in this article.

Traceback = prints functions after an error occurs; otherwise, nothing will print.

Debug = marks a line when called for debug mode.

Browser = When called, it suspends the function’s execution and puts it in debugging mode.

Recover = allows you to modify the error behavior so that you can browse the function call stack

Using Debugging Tools

Let’s use a few debugging functions we have learned now in projects. You can practice using these functions in your own projects.

r

gt; summary(x)
Error in summary(x) : object 'x' not found
r

gt; traceback()
1: summary(x)
With this function, you can print out the function you get an error. This function shows you in which function the error occurred.

r

gt; debug(functionName)
r

gt; functionName
Browse[2]> n , Q , c
When you call the function you want with this code block, you can switch to debugging mode. n command is executed the current expression and moves to the next expression, c-command is continued execution of the function and does not stop until either an error or the function exits, Q is the exit debugging mode.

CONGRATULATIONS, YOU FINISHED TO DEBUGGING IN R!

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Programming

This article is prepared to explain concepts that will be important for programming beginners such as data types, variables, heap and stack, and operators....

Programming

This article was created for those who want to use functional programming in their career or who are learning new programming paradigms. We will...

Programming

Functions are pieces of code that combine the integrity of the code used to perform a task into a single structure. If you are...

Programming

Computers can solve an event in different ways, we call these ways algorithms, each algorithm works differently from the others and has different processing...