What is the difference between warnings and messages?

Viewed 289

I have read http://adv-r.had.co.nz/Exceptions-Debugging.html and what I have understood is that we have three main types of conditions (that I will call bugs):

  • error
  • warning
  • message

As far as I understand error is something very inappropriate which can be very harmful to our function (the most serious type of bug). But I cannot distinguish warning from message. I would say that a message share additional information about some possible bad parts of our code, but which does not affect our code directly (it means that our code will work, but there are some additional information that we should notice).

  • What is exactly the difference between warning and massage?
  • Do I correctly understand what error means?
1 Answers

First you should be clear on what you mean by a "bug". A bug is simply a part of your code that doesn't act in the way you intended it to. Your code might have a bug but not give any errors, warnings, or messages. For example, the following function has an obvious bug:

add_two_numbers <- function(a, b)
{
  return(a - b);
}

add_two_numbers(2, 2)
#> [1] 0

Whoops! I wanted it to add the numbers but my function subtracted them instead. That's a bug, and if I hadn't found it at this stage, then I might run into trouble later if I rely on it to perform calculations.

Errors, warnings and messages are not bugs. They are intentional. They are put in place by the author of a function or package to ensure that the software does its job as intended, and alerts the user if they attempt to do something that the software is not intended to do or otherwise cannot perform.

Errors are normally used when a function has to stop calculating and just abort. This can be useful when testing inputs to a function:

add_two_numbers <- function(a, b) {
  if(!is.numeric(a)) stop(paste(a, "is not a number"))
  if(!is.numeric(b)) stop(paste(b, "is not a number"))
  
  return(a + b)
}

add_two_numbers("hello", 2)
#> Error in add_two_numbers("hello", 2): hello is not a number

add_two_numbers(2, "world")
#> Error in add_two_numbers(2, "world"): world is not a number

add_two_numbers(2, 2)
#> [1] 4

You'll notice that the first two calls to this function didn't return anything - they just emitted an error and stopped.

The reason we might want to do this is to try to give the user some useful guidance. For example, if we just had:

add_two_numbers <- function(a, b) a + b

Then when we try to pass the wrong argument types, we get this:

add_two_numbers("hello", 2)
#> Error in a + b : non-numeric argument to binary operator

This error was thrown by the R parser, and is a bit vague compared to our specific messages. When we write our own error messages, we are trying to predict when a more fundamental error will occur and head that off, to prevent it from happening and giving useful information to our user.

Similarly, we can write a warning message. This is usually used when we want to make the user aware of something that may give unexpected results. For example:

as.numeric(c("1", "2", "3", " "))
#> [1]  1  2  3 NA
#> Warning message:
#> NAs introduced by coercion

Did we want our empty space to be converted to NA, or is the empty space a data entry mistake? Either could be true, so the function continues to process the data and gives an output, but warns you that it might not be what you expected.

If we want a function that emits a warning, it is easy to write one:

pointless <- function() {
  warning("This function is pointless")
  return(1)
}

pointless()
#> [1] 1
#> Warning message:
#> In pointless() : This function is pointless

So our function still returns an answer, but it gives us a prominent warning whenever we use it.

Finally, messages are just that - messages to the user that do not form part of the output of a function, and are not necessarily bad, but write a message to the console that might be useful information.

add_two_numbers <- function(a, b) {
  message("Have a nice day")
  return(a + b)
}

add_two_numbers(2, 2)
#> Have a nice day
#> [1] 4
Related