Similar to other programming languages, R provides developers with an error-handling mechanism. However, the error-handling mechanism in R is implemented in the function instead of a pure code block. This is due to the fact that all operations are pure function calls.
In the first step, we demonstrate what will output if we add an integer to a string. If the operation is invalid, the system will print an error message on the console. There are three basic types of error handling messages in R, which are error
, warning
, and interrupt
.
Next, we create a function named addnum
, which is designed to return the addition of two arguments. However, sometimes you will pass an unexpected type of input (for example, string) into a function. For this condition, we can add an argument type check condition before the return
statement. If none of the input data types is numeric, the stop
function will print an error message quoted in the stop
function.
Besides using the stop
function, we can use a warning
function instead to handle an error. However, only using a warning function, the function process will not terminate but proceed to return a + b
. Thus, we might find both an error and warning message displayed on the console. To suppress the warning message, we can set warn=2
in the options
function, or we can use suppressWarnings
instead to mute the warning message. On the other hand, we can also use the stopifnot
function to check whether the argument is valid or not. If the input argument is invalid, we can stop the program and print an error message on the screen.
Moving on, we can catch the error using the try
function. Here, we store the error message into errormsg
in the operation of adding a character string to an integer. However, the function will still print the error message on the screen. We can mute the message by setting a silent
argument to TRUE
. Furthermore, the try
function is very helpful if don't want a for-loop being interrupted by unexpected errors. Therefore, we first demonstrate how an error may unexpectedly interrupt the loop execution. In that step, we may find that the loop execution stops, and we have successfully assigned only three variables to res
. However, we can actually proceed with the for-loop execution by wrapping the code into a try
function.
Besides the try
function, we can use a more advanced error-handling function, tryCatch
, to handle errors including warning
and error
. We use the tryCatch
function in the following manner:
In this function, we can catch warning
and error
messages in different function code blocks. By following the function form, we can create a function named dividenum
. The function first performs numeric division; if any error occurs, we can catch the error and print an error message in the error
function. At the end of the block, we remove any defined value within the function and print the message of clean variable
. At this point, we can test how this function works in three different situations: performing a normal division, dividing a string from a string, and passing only one parameter into the function. We can now observe the output message under different conditions. In the first condition, the function prints out the division result, followed by clean variable
because it is coded in the block of finally
. For the second condition, the function first catches the error of missing value in the error
block and then outputs clean variable
at the end. For the last condition, while we do not catch the error of not passing a value to the b
parameter, the function still returns an error message first and then prints clean variable
on the console.
If you want to catch the error message while using the tryCatch
function, you can put a conditionMessage
to the error argument of the tryCatch
function:
In this example, if you pass two valid numeric arguments to the dividenum
function, the function returns a computation of 3/5 as output. On the other hand, if you pass a non-numeric value to the function, the function catches the error with the conditionMessage
function and returns the error as the function output.