assert_values: Display an error if not the allowed values

Description Usage Arguments Details Value Examples

Description

This function calls the error() function to display an error if the variable's values are not valid.

Usage

 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
assert_na(
  x,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

assert_in(
  x,
  values,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

assert_in_range(
  x,
  min = NULL,
  max = NULL,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

assert_char_length(
  x,
  n = NULL,
  n_min = NULL,
  n_max = NULL,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

Arguments

x

(any) The variable to check.

level

(integer, optional) The level of the message, from 1 to 10. Default: 1.

msg_level

(integer, optional) The maximum level of messages to output. Default: set in the option "msgr.level".

msg_types

(character, optional) The type to write or display. Must either NULL or one or more from "INFO", "WARNING" or "ERROR". Default: set in the option "msgr.types".

log_path

(character, optional) The file path to the text log file. If set to "", then no logs are written. Default: set in the option "msgr.log_path".

values

(character) The allowed values.

min

(numeric, optional) The minimum allowed value for a range. If NULL the minimum value is not checked. Default: NULL.

max

(numeric, optional) The maximum allowed value for a range. If NULL the maximum value is not checked. Default: NULL.

n

(integer, optional) The allowed length. If NULL the exact character length is not checked. Default: NULL.

n_min

(integer, optional) The allowed minimum length. If NULL the minimum character length is not checked. Default: NULL.

n_max

(integer, optional) The allowed maximum length. If NULL the maximum character length is not checked. Default: NULL.

Details

The following validations can be checked:

The following values can be checked:

Value

If assertion passes then TRUE is returned. This allows you to make multiple assertions separated by &.

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
## Not run: 

# No error
assert_na(NA)
assert_na(c(NA, NA, NA))
# Error
assert_na(1)
assert_na(c(1, NA, 3))

# No error
assert_in(1:3, 1:10)
assert_in(c("A", "B"), LETTERS)
# Error
assert_in(8:12, 1:10)
assert_in(c("A", "B"), letters)

# No error
assert_in_range(1:3, min = 1)
assert_in_range(1:3, min = 1, max = 10)
# Error
assert_in_range(1:3, min = 5)
assert_in_range(1:3, min = 1, max = 2)

# Error
assert_char_length(c("bob", "jan"), n = 3)
assert_char_length(c("bob", "jan"), n_min = 1)
assert_char_length(c("bob", "jan"), n_min = 1, n_max = 10)
# No error
assert_char_length(c("bob", "jane"), n = 3)
assert_char_length(c("bob", "jane"), n_min = 5)
assert_char_length(c("bob", "jane"), n_min = 1, n_max = 2)


## End(Not run)

ChadGoymer/msgr documentation built on April 10, 2021, 10:31 a.m.