-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommon.R
More file actions
163 lines (132 loc) · 5.4 KB
/
common.R
File metadata and controls
163 lines (132 loc) · 5.4 KB
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
library(tidyverse)
library(stringr)
source("ErrorHandler.R")
studies <- data_frame(file = dir(path = "data_specifications")) %>%
mutate(file = str_replace(file, ".yaml", "")) %>%
separate(file, into = c("study", "format"))
# Main Validation Function
validate_dataset_field <- function(dataset_contents, field) {
if (field$required) {
if (field$field %in% names(dataset_contents)) {
if (any(is.na(dataset_contents[[field$field]])) && !field$NA_allowed) {
cat(sprintf("Dataset has blank or NA for required variable: '%s'.\n", field$field))
return(list(FALSE, NA))
}
if (field$type == "options") {
return(ValidateOption(dataset_contents, field))
} else if (field$type == "numeric") {
return(ValidateNumeric(dataset_contents, field))
} else if (field$type == "string") {
return(ValidateString(dataset_contents, field))
}
} else {
cat(sprintf("Dataset is missing required variable: '%s'.\n", field$field))
return(list(FALSE, NA))
}
}
return(list(TRUE, NA))
}
# Validate "options" type - C
ValidateOption <- function(dataset_contents, field) {
options <- if (is.list(field$options)) {
names(unlist(field$options, recursive = FALSE))
} else {
field$options
}
invalid_values <- setdiff(unique(dataset_contents[[field$field]]), options)
if (field$NA_allowed) {
invalid_values <- na.omit(invalid_values)
}
if (length(invalid_values) > 0) {
incorrect <- list(
column = field$field,
invalid_value = invalid_values
)
cat(sprintf("Dataset has wrong type for option variable '%s'. To view these errors, please download the highlighted errors sheet on the left.\n", field$field))
return(list(FALSE, incorrect))
}
return(list(TRUE, NA))
}
# Validate "numeric" type - C
ValidateNumeric <- function(dataset_contents, field) {
field_contents <- dataset_contents[[field$field]]
invalid_content <- c()
numeric_values <- suppressWarnings(as.numeric(field_contents))
non_numeric_indices <- which(is.na(numeric_values) & !is.na(field_contents))
if (length(non_numeric_indices) > 0) {
cat(sprintf("Dataset has wrong type for numeric variable '%s'. To view these errors, please download the highlighted errors sheet on the left.\n", field$field))
invalid_content <- c(invalid_content, field_contents[non_numeric_indices])
}
if (field$format == "restricted") {
numeric_values <- numeric_values[!is.na(numeric_values)]
lowerLimit <- as.numeric(field$lowerlimit)
upperLimit <- as.numeric(field$upperlimit)
below_lower <- numeric_values[numeric_values < lowerLimit]
above_upper <- numeric_values[numeric_values > upperLimit]
if (length(below_lower) > 0) {
cat(sprintf("Dataset has data points below the lower limit for numeric variable '%s'. To view these errors, please download the highlighted errors sheet on the left.\n", field$field))
invalid_content <- c(invalid_content, below_lower)
}
if (length(above_upper) > 0) {
cat(sprintf("Dataset has data points above the upper limit for numeric variable '%s'. To view these errors, please download the highlighted errors sheet on the left.\n", field$field))
invalid_content <- c(invalid_content, above_upper)
}
}
if (length(invalid_content) > 0) {
incorrect <- list(
column = field$field,
invalid_value = invalid_content
)
return(list(FALSE, incorrect))
}
return(list(TRUE, NA))
}
# Validate "string" type
ValidateString <- function(dataset_contents, field) {
field_contents <- dataset_contents[[field$field]]
invalid_value <- c()
if (field$format == "uncapitalized") {
has_upper <- grepl("[[:upper:]]", field_contents)
if (any(has_upper)) {
cat(sprintf("Dataset has an uppercase letter in lowercase-only variable '%s'. To view these errors, please download the highlighted errors sheet on the left.\n", field$field))
invalid_value <- c(invalid_value, field_contents[has_upper])
}
}
if (!is.na(field$lowerlimit)) {
lowerLimit <- as.numeric(field$lowerlimit)
short_strings <- field_contents[nchar(field_contents) < lowerLimit]
if (length(short_strings) > 0) {
cat(sprintf("Dataset has inputs shorter than the lower character limit for variable '%s'. To view these errors, please download the highlighted errors sheet on the left.\n", field$field))
invalid_value <- c(invalid_value, short_strings)
}
}
if (!is.na(field$upperlimit)) {
upperLimit <- as.numeric(field$upperlimit)
long_strings <- field_contents[nchar(field_contents) > upperLimit]
if (length(long_strings) > 0) {
cat(sprintf("Dataset has inputs longer than the upper character limit for variable '%s'. To view these errors, please download the highlighted errors sheet on the left.\n", field$field))
invalid_value <- c(invalid_value, long_strings)
}
}
if (length(invalid_value) > 0) {
incorrect <- list(
column = field$field,
invalid_value = invalid_value
)
return(list(FALSE, incorrect))
}
return(list(TRUE, NA))
}
# Validate dataset
validate_dataset <- function(fields, dataset_contents) {
issues <- list()
results <- TRUE
for(i in fields){
result <- validate_dataset_field(dataset_contents, i)
if (!result[[1]]) {
results <- FALSE
issues <- append(issues,result[[2]])
}
}
return(list(results,issues))
}