Module 11 – Debugging and defensive programming in R

Our assignment this week was to debug a block of code.

The original code was:

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) }

To start debugging, I pasted the code into RStudio to see what kind of error it would throw. I got:

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

The “unexpected symbol” error made me think that some brackets or parentheses weren’t closed or were misplaced. That wasn’t it, but it turned out to be something similar – looking at the last line, I noticed that the “return” looked out of place. It should have been on a separate line, by itself. That would certainly make it an unexpected symbol. I moved return to the next line, making the code look like:

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) }

At that point, the code ran successfully and the function was created.

Using the function was a different story – I passed it a built-in list as an argument, and got a new error:

Error in tukey.outlier(x[, j]) : could not find function "tukey.outlier"

I’m guessing that tukey.outlier() is a function that returns TRUE or FALSE depending on whether the value is an outlier. The code runs fine without that function (albeit it makes tukey.multiple() always return a bunch of TRUEs), so I assume this code will work provided tukey.outlier() is defined.

This entry was posted in R Programming. Bookmark the permalink.

Comments are closed.