# We create a main user-level function
# The computation is done by an internal function
# Here we compare stop_up with a regular stop
main_function = function(x = 1, y = 2){
my_internal_function(x, y)
}
my_internal_function = function(x, y){
if(!is.numeric(x)){
stop_up("Argument 'x' must be numeric but currently isn't.")
}
# Now regular stop
if(!is.numeric(y)){
stop("Argument 'y' must be numeric but currently isn't.")
}
nx = length(x)
ny = length(y)
if(nx != ny){
# Note that we use string interpolation with {}
warn_up("The lengths of x and y don't match: {nx} vs {ny}.")
}
x + y
}
# Let's compare the two error messages
# stop_up:
try(main_function(x = "a"))
# => the user understands that the problem is with x
# Now compare with the regular stop:
try(main_function(y = "a"))
# Since the user has no clue of what my_internal_function is,
# s/he will be puzzled of what to do to sort this out
# Same with the warning => much clearer with warn_up
main_function(1, 1:2)
Run the code above in your browser using DataLab