'DEBUGGING' in R

The code below contains a 'deliberate' bug that must be rectified.

tukey_multiple <- function(x) {
  outliers <- array(TRUE,dim=dim(x))
  for (j in 1:ncol(x))
  {
    outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])
  }
  outlier.vec <- vector(length=nrow(x))
  for (i in 1:nrow(x))
  { outlier.vec[i] <- all(outliers[i,]) } return(outlier.vec) }

When the above code is implemented, it will result in the following error:

Error: unexpected symbol in:
" for (i in 1:nrow(x))
{ outlier.vec[i] <- all(outliers[i,]) } return"

The function 'traceback' prints the list of functions that were called before the error occurred.

The function 'source', which reads and executes the code, can be used to find the exact location of the error, which will help to rectify the error.

So, when the 'source' function is implemented, it gives the location of the error just before 'return'.


> traceback("tukey_multiple")
1: tukey_multiple
> source("tukey_multiple.R")
Error in source("tukey_multiple.R") : 
  tukey_multiple.R:9:43: unexpected symbol
8:   for (i in 1:nrow(x))
9:   { outlier.vec[i] <- all(outliers[i,]) } return
                                             ^
This error can be be overcome by placing 'return(outlier.vec)}' in the next line which will isolate 
it from 'for()' loop.

tukey_multiple <- function(x) {
outliers <- array(TRUE,dim=dim(x))
for (j in 1:ncol(x))
{
outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])
}
outlier.vec <- vector(length=nrow(x))
for (i in 1:nrow(x))
{ outlier.vec[i] <- all(outliers[i,]) }
return(outlier.vec) }

When 'debug' function is implemented on the code 'tukey_multiple',following error is noted which 
states that 'x' is missing. So, 'x' argument needs to be defined.

> debug(tukey_multiple)
> tukey_multiple()
debugging in: tukey_multiple()
debug at #1: {
    outliers <- array(TRUE, dim = dim(x))
    for (j in 1:ncol(x)) {
        outliers[, j] <- outliers[, j] && tukey.outlier(x[, j])
    }
    outlier.vec <- vector(length = nrow(x))
    for (i in 1:nrow(x)) {
        outlier.vec[i] <- all(outliers[i, ])
    }
    return(outlier.vec)
}
Browse[2]> n
debug at #2: outliers <- array(TRUE, dim = dim(x))
Browse[2]> n
Error in array(TRUE, dim = dim(x)) : 
  argument "x" is missing, with no default
Using function 'matrix', a 3*4 matrix  is generated with normally random distributed values. Function 'debug' is implemented on the code followed by calling function 'tukey_multiple'  with matrix.
> matrix <- matrix(rnorm(12), nrow = 3, ncol = 4)
> debug(tukey_multiple)
> tukey_multiple(matrix)
debugging in: tukey_multiple(matrix)
debug at #1: {
    outliers <- array(TRUE, dim = dim(x))
    for (j in 1:ncol(x)) {
        outliers[, j] <- outliers[, j] && tukey.outlier(x[, j])
    }
    outlier.vec <- vector(length = nrow(x))
    for (i in 1:nrow(x)) {
        outlier.vec[i] <- all(outliers[i, ])
    }
    return(outlier.vec)

> debug(tukey_multiple) > tukey_multiple(matrix) debugging in: tukey_multiple(matrix)
debug at tukey_multiple.R#1: {
outliers <- array(TRUE, dim = dim(x))
for (j in 1:ncol(x)) {
outliers[, j] <- outliers[, j]
}
outlier.vec <- vector(length = nrow(x))
for (i in 1:nrow(x)) {
outlier.vec[i] <- all(outliers[i, ])
}
return(outlier.vec)
}
Browse[2]> n debug at tukey_multiple.R#2: outliers <- array(TRUE, dim = dim(x))
Browse[2]> n debug at tukey_multiple.R#3: for (j in 1:ncol(x)) {
outliers[, j] <- outliers[, j]
}
Browse[2]> n debug at tukey_multiple.R#5: outliers[, j] <- outliers[, j]
Browse[2]> n debug at tukey_multiple.R#5: outliers[, j] <- outliers[, j]
Browse[2]> n debug at tukey_multiple.R#5: outliers[, j] <- outliers[, j]
Browse[2]> n debug at tukey_multiple.R#5: outliers[, j] <- outliers[, j]
Browse[2]> n debug at tukey_multiple.R#7: outlier.vec <- vector(length = nrow(x))
Browse[2]> n debug at tukey_multiple.R#8: for (i in 1:nrow(x)) {
outlier.vec[i] <- all(outliers[i, ])
}
Browse[2]> n debug at tukey_multiple.R#9: outlier.vec[i] <- all(outliers[i, ])
Browse[2]> n debug at tukey_multiple.R#9: outlier.vec[i] <- all(outliers[i, ])
Browse[2]> n debug at tukey_multiple.R#9: outlier.vec[i] <- all(outliers[i, ])
Browse[2]> n debug at tukey_multiple.R#10: return(outlier.vec)
Browse[2]> n exiting from: tukey_multiple(matrix)
[1] TRUE TRUE TRUE



In this way, implementing above codes and rectifying all the errors, a code can be DEBUGGED.

URL to git repo:https://github.com/VedaVangala/vedas-r-repo/tree/main/R11

References

Wickham, H. (2019). Debugging, condition handling, and defensive programming. Advanced R. CRC Press, Boca Raton, FL (2019). ISBN: 978-0815384571. http://adv-r.had.co.nz/Exceptions-Debugging.html

Matloff, N. (2011). The Artof R programming.(Chapter 13)


Comments

Popular posts from this blog

Input/Output, String manipulation and 'plyr' package in R

PACKAGE "ACCURACY"

Visualization of Graphics in R