View source: R/verifyFunctionArguments.R
verifyFunctionArguments | R Documentation |
Use this function to verify function arguments.
verifyFunctionArguments(arguments_l, abort_b_1 = TRUE, verbosity_b_1 = FALSE)
arguments_l |
An unconstrained |
abort_b_1 |
A single |
verbosity_b_1 |
A single |
This function allows to check all parameter types and values in a single line of code.
See examples below to know how to put this function in action.
Returned value depends on parameter abort_b_1
value.
When set to TRUE
, any error will abort processing by issuing a call to
stop
function.
When set to FALSE
, returned value is a boolean. It is TRUE
only
when no error have been detected. Otherwise FALSE
.
This function whenever used, should be the first statement of your function code.
Using this function outside function code is a non-sense.
Fabien Gelineau <neonira@gmail.com>
Maintainer: Fabien Gelineau <neonira@gmail.com>
fun <- function(values_i_3m) {
verifyFunctionArguments(mget(ls()), FALSE, FALSE)
}
fun(1)
# [1] FALSE
fun(1:7)
# [1] TRUE
nonOPFun <- function(x) {
verifyFunctionArguments(mget(ls()), FALSE, TRUE)
}
nonOPFun(1:7)
# $x
# [1] 1 2 3 4 5 6 7
#
# x FALSE unknown suffix, [NA]
#
# [1] FALSE
# real use case with abortion
myFunWithAbortion <- function(values_i_3m) {
verifyFunctionArguments(mget(ls()))
# ...
}
tryCatch(myFunWithAbortion(1), error = function(e) cat(e$message, '\n'))
# argument mistmatch [values_i_3m] wrong length, was expecting [3m] , got [1]
# real use case without abortion
myFunWithoutAbortion <- function(values_i_3m) {
if (!verifyFunctionArguments(mget(ls()), FALSE)) return(FALSE)
cat('continuing processing ...\n')
TRUE
}
myFunWithoutAbortion(1)
# FALSE
myFunWithoutAbortion(1:3)
# continuing processing ...
# TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.