message() vs. warning() vs. stop() Functions in R (4 Examples)


This article shows how to return warning and error messages in the R programming language.

The article will contain four examples for the application of the message, warning, and stop functions. More precisely, the content of the article is structured as follows:

It’s time to dive into the examples:

Definitions & Basic R Syntaxes of message, warning & stop Functions


Definitions: You can find the definitions of the message, warning, and stop functions below.

  • The message R function generates a diagnostic message.
  • The warning R function generates a warning message.
  • The stop R function generates an error message and stops executing the current R code.


Basic R Syntaxes: You can find the basic R programming syntaxes of the message, warning, and stop functions below.

message(any_string)                     # Basic R syntax of message function
warning(any_string)                     # Basic R syntax of warning function
stop(any_string)                        # Basic R syntax of stop function


In the remaining tutorial, I’ll show four examples for the application of the message, warning, and stop functions and commands in R. So keep on reading!

Example 1: Apply message() Function in R

Example 1 explains how to use the message function in the R programming language. Within the message function, we need to specify a character string that should be returned as diagnostic message to the RStudio console:

message("This is a message")            # Using message function
# This is a message

Have a look at the previous output of the RStudio console: We returned a diagnostic message.


Example 2: Apply warning() Function in R

In this Example, I’ll show how to apply the warning function. Similar to the message function, we need to give a character string as input for the warning command:

warning("This is a warning message")    # Using warning function
# Warning message:
# This is a warning message

By comparing the previous RStudio console output with the output of Example 1, you can see the major difference between the message and warning functions: The warning function returns another line of output saying “Warning message”. This indicates that there might be a problem with the R syntax.


Example 3: Apply stop() Function in R

The following R code illustrates how to generate error messages using the stop function. Again, we need to assign a character string to the stop function:

stop("This is an error message") # Using stop function
# Error: This is an error message

As you can see, the stop function added the term “Error:” in front of our character string.

However, this is not the only difference of the stop function compared to the message and warning functions. So keep on reading!


Example 4: Using stop() Function to Interrupt Process

The following R syntax shows another important feature of the stop function: The stop function stops the execution of the currently running R code. I’ll illustrate that with a for-loop containing of ten iterations:

for(i in 1:10) {                        # For-loop containing error condition
  if(i != 5) {
    print(paste("Finished loop iteration No.", i))
  if(i == 5) {
    stop("i was equal to 5!")
# [1] "Finished loop iteration No. 1"
# [1] "Finished loop iteration No. 2"
# [1] "Finished loop iteration No. 3"
# [1] "Finished loop iteration No. 4"
# Error: i was equal to 5!

Have a look at the previous output. As you can see our for-loop stopped running at iteration No. 5. The reason for that is that we specified an error condition within the for-loop, which stopped the R code in case iteration 5 was reached.


Video, Further Resources & Summary

Do you need further info on the R programming code of this article? Then you might want to watch the following video of my YouTube channel. I show the R programming codes of this article in the video.


The YouTube video will be added soon.


Also, I can recommend to read the other tutorials on this homepage:


This tutorial illustrated how to apply the message, warning, and stop functions in R programming. Let me know in the comments section, if you have any additional questions. Furthermore, please subscribe to my email newsletter to receive updates on new articles.


Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.