assert_type: Display an error if not the correct type

Description Usage Arguments Details Value Examples

View source: R/assert.R

Description

This function calls the error() function to display an error if the variable type of the input is not correct. Depending on the type, a number of other attributes can be checked simultaneously, such as length, using n, or size of rows and columns, using n_row and n_col respectively.

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
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
assert_type(
  x,
  type,
  n = NULL,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

assert_class(
  x,
  class,
  n = NULL,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

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

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

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

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

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

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

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

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

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

assert_factor(
  x,
  levels = NULL,
  n = NULL,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

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

assert_array(
  x,
  dims = NULL,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

assert_matrix(
  x,
  n_col = NULL,
  n_row = NULL,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

assert_data_frame(
  x,
  n_col = NULL,
  n_row = NULL,
  level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

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

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

Arguments

x

(any) The object to test.

type

(character) The allowed type.

n

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

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".

class

(character) The allowed class.

levels

(character, optional) The allowed levels. If NULL the levels are not checked. Default: NULL.

dims

(integer, optional) The allowed dimension sizes. If NULL the dimensions are not checked. Default: NULL.

n_col

(integer, optional) The allowed number of columns. If NULL the columns are not checked. Default: NULL.

n_row

(integer, optional) The allowed number of rows. If NULL the rows are not checked. Default: NULL.

Details

The following types 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
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
## Not run: 

# No error
assert_type(data.frame(x = 1:3), "list")
assert_type(list(1, 2, 3), "list", n = 3)
# Error
assert_type(1:3, "list")
assert_type(1:3, "integer", n = 1)

# No error
assert_class(data.frame(x = 1:3), "data.frame")
assert_class(list(1, 2, 3), "list", n = 3)
# Error
assert_class(data.frame(x = 1:3), "list")
assert_class(1:3, "integer", n = 1)

# No error
assert_null(NULL)
# Error
assert_null(1)

# No error
assert_atomic(1:3)
assert_atomic(1:3, n = 3)
# Error
assert_atomic(list(1))
assert_atomic(1:3, n = 1)

# No error
assert_vector(1:3)
assert_vector(1:3, n = 3)
# Error
assert_vector(options)
assert_vector(1:3, n = 1)

# No error
assert_logical(TRUE)
assert_logical(c(TRUE, FALSE, TRUE), n = 3)
# Error
assert_logical(1)
assert_logical(c(TRUE, FALSE, TRUE), n = 1)

# No error
assert_integer(1:3)
assert_integer(1:3, n = 3)
# Error
assert_integer(c(1.414, 3.142))
assert_integer(1:3, n = 1)

# No error
assert_natural(c(1, 2, 3))
assert_natural(1:3, n = 3)
# Error
assert_natural(-1:3)
assert_natural(c(1.414, 3.142))
assert_natural(1:3, n = 1)

# No error
assert_double(c(1.414, 1.732, 2.000))
assert_double(c(1.414, 1.732, 2.000), n = 3)
# Error
assert_double(c("A", "B", "C"))
assert_double(c(1.414, 1.732, 2.000), n = 1)

# No error
assert_number(c(1.414, 1.732, 2.000))
assert_number(c(1.414, 1.732, 2.000), n = 3)
# Error
assert_number(c("A", "B", "C"))
assert_number(c(1.414, 1.732, 2.000), n = 1)

# No error
assert_character(c("A", "B", "C"))
assert_character(c("A", "B", "C"), n = 3)
# Error
assert_character(1:3)
assert_character(c("A", "B", "C"), n = 1)

# No error
assert_factor(factor(c("A", "B", "A")))
assert_factor(factor(c("A", "B", "A")), n = 3)
assert_factor(factor(c("A", "B", "A")), levels = c("A", "B"))
# Error
assert_factor(1:3)
assert_factor(factor(c("A", "B", "A")), n = 1)
assert_factor(factor(c("A", "B", "A")), levels = c("B", "C"))

# No error
assert_list(list(1, 2, 3))
assert_list(list(1, 2, 3), n = 3)
# Error
assert_list(c(1, 2, 3))
assert_list(list(1, 2, 3), n = 1)

# No error
assert_data_frame(data.frame(x = 1:26, y = LETTERS))
assert_data_frame(data.frame(x = 1:26, y = LETTERS), n_col = 2)
assert_data_frame(data.frame(x = 1:26, y = LETTERS), n_row = 26)
assert_data_frame(data.frame(x = 1:26, y = LETTERS), n_col = 2, n_row = 26)
# Error
assert_data_frame(list(x = 1:26, y = LETTERS))
assert_data_frame(data.frame(x = 1:26, y = LETTERS), n_col = 1)
assert_data_frame(data.frame(x = 1:26, y = LETTERS), n_row = 10)
assert_data_frame(data.frame(x = 1:26, y = LETTERS), n_col = 1, n_row = 10)

# No error
assert_function(options)
# Error
assert_function(1:3)

# No error
assert_formula(x ~ 1)
# Error
assert_formula(x == 1)


## End(Not run)

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