verifyFunctionArguments: Function verifyFunctionArguments

Description Usage Arguments Details Value Note Author(s) Examples

View source: R/verifyFunctionArguments.R

Description

Use this function to verify function arguments.

Usage

1
verifyFunctionArguments(arguments_l, abort_b_1 = TRUE, verbosity_b_1 = 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.

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.

Value

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.

Note

This function whenever used, should be the first statement of your function code.

Using this function outside function code is a non-sense.

Author(s)

Fabien Gelineau <neonira@gmail.com>

Maintainer: Fabien Gelineau <neonira@gmail.com>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

neonira/wyz.code.offensiveProgramming documentation built on Feb. 20, 2020, 2:01 p.m.