Connect with us

Hi, what are you looking for?

Debugging in R
Debugging in R

Data Science

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

clean code is essential for updating scripts, adding new features to code, etc. So every developer, must learn how to clean the code. With...

Daily News

Cloud computing is a system that is often heard today and that most developers use at least once. In this article, we’ll look at...

Game Development

Visual Studio Code without a doubt, most popular and loved code editor in the code editor market. Many developers love working with Vs Code....

Daily News

GitHub Copilot was introduced last year and continues to make it easier for developers with nice updates to date. Although most people see Github...