-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchecks.R
258 lines (200 loc) · 6.51 KB
/
checks.R
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#' Check the existence of text files in a GTFS object
#'
#' Checks the existence of elements inside a GTFS object that represent specific
#' GTFS text files.
#'
#' @param x A GTFS object.
#' @param files A character vector. The files to check the existence of.
#'
#' @return
#' \code{check_file_exists} returns \code{TRUE} if the check is successful, and
#' \code{FALSE} otherwise. \cr
#' \code{assert_file_exists} returns \code{x} invisibly if the check is
#' successful, and throws an error otherwise.
#'
#' @family checking functions
#'
#' @examples
#' gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
#' gtfs <- import_gtfs(gtfs_path)
#'
#' check_file_exists(gtfs, c("calendar", "agency"))
#'
#' check_file_exists(gtfs, c("calendar", "oi"))
#'
#' @export
check_file_exists <- function(x, files) {
assert_class(x, "gtfs")
assert_vector(files, "character")
# actual checking
missing_files <- setdiff(files, names(x))
if (identical(missing_files, character(0))) return(TRUE)
return(FALSE)
}
#' @rdname check_file_exists
#' @export
assert_file_exists <- function(x, files) {
assert_class(x, "gtfs")
assert_vector(files, "character")
# actual checking
missing_files <- setdiff(files, names(x))
if (identical(missing_files, character(0))) return(invisible(x))
gtfsio_error(
paste0(
"The GTFS object is missing the following required element(s): ",
paste0("'", missing_files, "'", collapse = ", ")
),
subclass = "missing_required_file"
)
}
#' Check the existence of fields in a GTFS object element
#'
#' Checks the existence of fields, represented by columns, inside a GTFS object
#' element.
#'
#' @param x A GTFS object.
#' @param file A string. The element, that represents a GTFS text file, where
#' fields should be searched.
#' @param fields A character vector. The fields to check the existence of.
#'
#' @return
#' \code{check_field_exists} returns \code{TRUE} if the check is successful, and
#' \code{FALSE} otherwise. \cr
#' \code{assert_field_exists} returns \code{x} invisibly if the check is
#' successful, and throws an error otherwise.
#'
#' @family checking functions
#'
#' @examples
#' gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
#' gtfs <- import_gtfs(gtfs_path)
#'
#' check_field_exists(gtfs, "calendar", c("monday", "tuesday"))
#'
#' check_field_exists(gtfs, "calendar", c("monday", "oi"))
#'
#' @export
check_field_exists <- function(x, file, fields) {
assert_class(x, "gtfs")
assert_vector(file, "character", len = 1L)
assert_vector(fields, "character")
# check if 'file' exists, and return FALSE if it doesn't
if (!check_file_exists(x, file)) return(FALSE)
# checking for field existence
missing_fields <- setdiff(fields, names(x[[file]]))
if (identical(missing_fields, character(0))) return(TRUE)
return(FALSE)
}
#' @rdname check_field_exists
#' @export
assert_field_exists <- function(x, file, fields) {
assert_class(x, "gtfs")
assert_vector(file, "character", len = 1L)
assert_vector(fields, "character")
assert_file_exists(x, file)
# actual checking
missing_fields <- setdiff(fields, names(x[[file]]))
if (identical(missing_fields, character(0))) return(invisible(x))
gtfsio_error(
paste0(
"The GTFS object '",
file,
"' element is missing the following required column(s): ",
paste0("'", missing_fields, "'", collapse = ", ")
),
subclass = "missing_required_field"
)
}
#' Check the classes of fields in a GTFS object element
#'
#' Checks the classes of fields, represented by columns, inside a GTFS object
#' element.
#'
#' @param x A GTFS object.
#' @param file A string. The element, that represents a GTFS text file, whose
#' fields' classes should be checked.
#' @param fields A character vector. The fields to have their classes checked.
#' @param classes A character vector, with the same length of \code{fields}. The
#' classes that each field must inherit from.
#'
#' @return
#' \code{check_field_class} returns \code{TRUE} if the check is successful, and
#' \code{FALSE} otherwise. \cr
#' \code{assert_field_class} returns \code{x} invisibly if the check is
#' successful, and throws an error otherwise.
#'
#' @family checking functions
#'
#' @examples
#' gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
#' gtfs <- import_gtfs(gtfs_path)
#'
#' check_field_class(
#' gtfs,
#' "calendar",
#' fields = c("monday", "tuesday"),
#' classes = rep("integer", 2)
#' )
#'
#' check_field_class(
#' gtfs,
#' "calendar",
#' fields = c("monday", "tuesday"),
#' classes = c("integer", "character")
#' )
#'
#' @export
check_field_class <- function(x, file, fields, classes) {
assert_class(x, "gtfs")
assert_vector(file, "character", len = 1L)
assert_vector(fields, "character")
if (!is.character(classes) | length(classes) != length(fields))
gtfsio_error(
"'classes' must be a character vector with the same length of 'fields'.",
subclass = "bad_classes_argument"
)
# check if 'fields' exists, and return FALSE if it doesn't
if (!check_field_exists(x, file, fields)) return(FALSE)
# checking - compare the desired classes to the actual classes
col_classes <- vapply(x[[file]], class, character(1))
actual_classes <- col_classes[fields]
if (all(classes == actual_classes)) return(TRUE)
return(FALSE)
}
#' @rdname check_field_class
#' @export
assert_field_class <- function(x, file, fields, classes) {
assert_class(x, "gtfs")
assert_vector(file, "character", len = 1L)
assert_vector(fields, "character")
assert_field_exists(x, file, fields)
if (!is.character(classes) | length(classes) != length(fields))
gtfsio_error(
"'classes' must be a character vector with the same length of 'fields'.",
subclass = "bad_classes_argument"
)
# actual checking - compare the desired classes to the actual classes
col_classes <- vapply(x[[file]], class, character(1))
actual_classes <- col_classes[fields]
if (all(classes == actual_classes)) return(invisible(x))
bad_fields <- fields[classes != actual_classes]
req_classes <- classes[classes != actual_classes]
act_classes <- actual_classes[classes != actual_classes]
gtfsio_error(
paste0(
"The following columns in the GTFS object '",
file,
"' element do not inherit from the required classes:\n",
paste0(
" - '",
bad_fields,
"': requires ",
req_classes,
", but inherits from ",
act_classes,
collapse = "\n"
)
),
subclass = "wrong_class_field"
)
}