I would like to share some some functions that I wrote in R with collegues in form of a an R-package. Those functions rely heavily on other packages and as a practitioneer without programming background, I'm having difficulties to draw a clear line on whether I need to test for a possible error in the usage of the function myself (and create an associated error message) or whether I can just leave this to the error messages of the packages that I'm using.
Here is a small example to make my point clearer:
Multiply_raster <- function(input_raster, multiplication_term) {
# Test for some things that might go wrong
if (is.element("raster", installed.packages()[, 1]) == F) {
stop(
"You do not have 'raster' installed. Please install the package before proceeding",
call. = F
)
}
if (class(input_raster) != "RasterLayer") {
stop("Please provide my_baselayer as an object of Class RasterLayer",
call. = F)
}
if (is.numeric(multiplication_term) == FALSE) {
stop("Error: Your multiplication term should be numeric", call. = F)
}
# load lib
library(raster)
# multply multiplication term with input raster
input_raster * multiplication_term
}
# data for Usage example
library(raster)
raster_example <- raster(matrix(rnorm(400), 20, 20))
First Test
# Error test 1
Multiply_raster(raster_example, "5")
Gives
> Multiply_raster(raster_example, "5")
Error: Error: Your multiplication term should be numeric
Second Test
# Error test 2
Multiply_raster(1:5, 5)
gives
> Multiply_raster(1:5, 5)
Error: Please provide my_baselayer as an object of Class RasterLayer
Alternative specification without error messages
# alternative function specification without own error messages
Multiply_raster_2 <-
function(input_raster, multiplication_term) {
# load lib
library(raster)
# multply multiplication term with input raster
input_raster * multiplication_term
}
First test
# Error test 1
Multiply_raster_2(raster_example, "5")
gives
> Multiply_raster_2(raster_example, "5")
Error in input_raster * multiplication_term :
non-numeric argument to binary operator
Second test
# Error test 2
Multiply_raster_2(1:5, 5)
gives
> Multiply_raster_2(1:5, 5)
[1] 5 10 15 20 25
My interpretation: In the first case of the error test I would not necessarily need the logical test and an own error message, because It is quite clear that the multiplication will only work with the correct type of data.
In the case of the second error message, I would need the test because otherwise R still evaluates the function although the input data was not a raster.
Relevance: I ask this because of two things: First, it takes quite some time and ressources to anticipate all possible errors that a user might commit when using the function. Second, I want to avoid redundancy especially if existing error message might be more to the point or better than my own.
I searched for answer on this topic in the tidyverse style-guide and in the Advanced R tips from Hadley Wickham but could not find a clear orientation on this.