This functions checks the evaluation of an expression and, if an error is thrown, captures it and integrates the captured message after a custom error message.

check_expr(expr, ..., clean, up = 0, arg_name, verbatim = FALSE)

check_expr_hook(expr, ..., clean, arg_name, verbatim = FALSE)

generate_check_expr_hook(namespace)

Arguments

expr

An expression to be evaluated.

...

Character scalars. The values of ... will be coerced with the function string_magic. This means that string interpolation is allowed. Ex: "Arg. {arg} should be positive" leads to "Arg. power should be positive" if arg is equal to "power".

If argument verbatim is TRUE, the values are instead coereced with paste0.

clean

Character vector, default is missing. If provided, the function string_clean is applied to the captured error message to clean it when necessary. Each element of the vector should be of the form "pat => rep" with pat a regular expression to be replace and rep the replacement.

up

Integer, default is 0. It is used to construct the call in the error message. By default the call reported is the function containing check_expr. If you want to report a function higher in the stack, use up = 1, or higher.

arg_name

Character scalar, default is missing. Used when the expression in expr leads to an error and the custom message is missing (i.e. no element is provided in ...). In that case, the default message is: "The argument {arg_name} could not be evaluated.". The default value for arg_name is deparse(substitute(expr)), if this guess is wrong, use arg_name.

verbatim

Logical scalar, default is FALSE. By default the elements of ... allow string interpolation with "{}" using stringmagic. If TRUE, no interpolation is performed.

namespace

Character scalar giving the namespace for which the hooks are valid. Only useful when hook functions are used in a package.

Details

The purpose of this functions is to provide useful error messages to the user.

Functions

  • check_expr_hook(): As check_expr but sets the error call at the level of the hooked function

  • generate_check_expr_hook(): Generates a package specific check_expr_hook function

See also

For general argument checking, see check_arg() and check_set_arg().

Author

Laurent Berge

Examples


test = function(x, y){
  check_expr(mean(x, y), "Computing the mean didn't work:")
}