Regarding R packages development, when the function should return an error message or a warning, what is best to use?:
cat()
print()
message()
error()
warning()
For example in this function, which one should I use to return an error message when a or b are not numbers.
function_sum_two_nums <- function(a, b){
## Check if the a and b are numbers
if(is.numeric(a) & is.numeric(b)){
return(a+b)
} else{
print/cat/message/error/warning('a or b are not numbers')
}
}
In this particular case, the only sensible option is to use stop()
, because this situation is an error, and you want to stop the function from continuing when the error is encountered.
The other options will allow code execution to proceed, returning the last evaluated expression in the function. This is not what you want.
Use print()
when you want to provide output for the end-user. Use cat()
for more control over output in the same situation. This is not suitable for "library code", because it cannot be suppressed easily. However, it is the correct way to perform tasks like emitting data to stdout for processing in a shell pipeline, or writing functions that "pretty print" interesting things.
Use message()
for informational messages that are not warnings or errors, but that arise during some computation where the user didn't specifically request output. For example, if you are developing a package, and you want to inform the user that they can install a special dependency to improve performance, you can use message()
to emit this message. Messages can be suppressed by the user with suppressMessages
.
Use warning()
to indicate problematic situations that do not require function execution to abort. An example would be if the user requested an action that would be very slow, or that might cause numerical stability problems in a later computation. Warnings can be suppressed by the user with suppressWarnings
.
As above, use error()
when something is so totally wrong that the function cannot continue executing. Errors can be caught and handled, (e.g. with tryCatch
), but the user must specifically make this happen.
The other functions: message
, warning
, and stop
each signal (or "throw") a condition. I will not go into detail on what conditions are, because Wickham covers them in chapter 8 of Advanced R.
The very simple version is the all 3 of these introduce a signal to the R runtime, that does nothing on its own. The user can then install a "handler" for a specific condition, which is a function that is invoked whenever the condition is signaled.
R has default handlers for the conditions signaled by message()
, warning(),
and error()
. This is how the standard behavior of those functions is implemented.
Actually, none of your listed examples since there are functions within base
especially for both (warning/error) cases:
warning('a or b are not numbers')
will result in an warning message but continue executesstop('a or b are not numbers')
stops executes and throws an errorIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With