Skip to contents

Assert that all elements of the argument are named.

Usage

assert_named(arg, optional = FALSE)

Arguments

arg

A function argument to be checked

optional

Is the checked argument optional? If set to FALSE and arg is NULL then an error is thrown

Value

The function throws an error if arg is not a named list or vector or returns the input invisibly otherwise

Examples

example_fun <- function(varval_list) {
  assert_named(varval_list)
}

example_fun(list(var1 = 1, var2 = "x"))

try(example_fun(list(1, "x")))
#> Error in assert_named(varval_list) : 
#>   All elements of `varval_list` must be named.
#> No element is named.

try(example_fun(list(var = 1, "x")))
#> Error in assert_named(varval_list) : 
#>   All elements of `varval_list` must be named.
#> The following elements are not named: 2