Learn R Programming

wyz.code.offensiveProgramming (version 1.1.24)

verifyFunctionArguments: Verify Function Arguments

Description

Use this function to verify function arguments.

Usage

verifyFunctionArguments(arguments_l, abort_b_1 = TRUE, verbosity_b_1 = FALSE)

Value

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.

Arguments

arguments_l

An unconstrained list, representing the arguments. Should always result from a call to mget(ls()).

abort_b_1

A single boolean value stating if processing abortion should be triggered in case of error

verbosity_b_1

A single boolean value.

Author

tools:::Rd_package_author("wyz.code.offensiveProgramming")

Maintainer: tools:::Rd_package_maintainer("wyz.code.offensiveProgramming")

Details

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.

Examples

Run this code
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

Run the code above in your browser using DataLab