-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.R
More file actions
233 lines (193 loc) · 8.05 KB
/
server.R
File metadata and controls
233 lines (193 loc) · 8.05 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
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
library(shiny)
library(tidyverse)
library(yaml)
source("common.R")
source("ErrorHandler.R")
# NEW Server
server <- function(input, output, session) {
output$study_format <- renderUI({
selectInput("format", label = h4("Study Format"),
choices = filter(studies, study == input$study)$format)
})
output$specification <- renderPrint({
req(input$study, input$format)
yaml_file_path <- paste0("data_specifications/", input$study, "_", input$format, ".yaml")
yaml::yaml.load_file(yaml_file_path)
})
output$validator_output <- renderPrint({
req(input$file)
yaml_file_path <- paste0("data_specifications/", input$study, "_", input$format, ".yaml")
fields <- yaml::yaml.load_file(yaml_file_path)
tryCatch({
df <- read_csv(input$file$datapath)
cat("Dataset uploaded successfully! Reviewing Dataset... \n\n")
}, error = function(e) {
stop(safeError(e))
})
validated <- validate_dataset(fields, df)
valid <- validated[[1]]
issues <- validated[[2]]
if (valid) {
cat("\nDataset is valid! All variables match specifications.")
} else {
cat("\nThe dataset is not valid. Please review the specifications and the highlighted error log.")
highlight_csv_to_xlsx(df, issues, "/Users/abteen/Desktop/issues.xlsx")
}
})
userData <- reactive({
nVars <- input$numVars
data_list <- list()
if (nVars > 0) {
for (i in 1:nVars) {
field_type <- input[[paste0("field_type_", i)]]
if(field_type == "numeric") {
lowerlimit <- ifelse(input[[paste0("range_req_", i)]] == "yes", input[[paste0("min_value_", i)]], NA)
upperlimit <- ifelse(input[[paste0("range_req_", i)]] == "yes", input[[paste0("max_value_", i)]], NA)
} else {
lowerlimit <- ifelse(field_type == "string" && input[[paste0("range_req_string", i)]] == "yes", input[[paste0("min_value_s", i)]], NA)
upperlimit <- ifelse(field_type == "string" && input[[paste0("range_req_string", i)]] == "yes", input[[paste0("max_value_s", i)]], NA)
}
options <- c()
if (field_type == "options"){
stringList <- strsplit(input[[paste0("option_input_", i)]], split = ",")
for (j in stringList) {
options <- c(options, j)
}
}
format <- if (field_type == "numeric" && input[[paste0("range_req_", i)]] == "yes") {
"restricted"
} else if (field_type == "string" && input[[paste0("caped_", i)]] == "yes") {
"capitalized"
} else if (field_type == "string" && input[[paste0("caped_", i)]] == "no") {
"uncapitalized"
} else {
"open"
}
required <- if(input[[paste0("is_required_", i)]] == 'yes') {
TRUE
} else {
FALSE
}
NA_allowed <- if(input[[paste0("allow_na_", i)]] == 'yes'){
TRUE
} else {
FALSE
}
data_list[[i]] <- list(
field = input[[paste0("field_name_", i)]],
description = input[[paste0("field_description_", i)]],
type = field_type,
options = options,
format = format,
lowerlimit = lowerlimit,
upperlimit = upperlimit,
required = required,
NA_allowed = NA_allowed,
error_message = toString(input[[paste0("error_message_", i)]])
)
}
}
return(data_list)
})
output$downloadSetup <- downloadHandler(
filename = function() {
paste("data_settings_", Sys.Date(), ".yaml", sep = "")
},
content = function(file) {
data <- userData()
write_yaml(data, file)
}
)
output$variableInputs <- renderUI({
numVars <- input$numVars
if (numVars > 0) {
field_list <- lapply(1:numVars, function(i) {
fluidRow(
# Field Humanize Variables
textInput(paste0("field_name_", i), paste("Enter the name of your data (Field ", i, "):")),
textInput(paste0("field_description_", i), paste("Enter a description of your data (Field ", i, "):")),
# Field Type and global variables
selectInput(paste0("field_type_", i), "Choose your data type:",
choices = c("options", "numeric", "string")),
selectInput(paste0("is_required_", i), "Is the data type required:",
choices = c("yes", "no")),
selectInput(paste0("allow_na_", i), "Are NA Values Allowed:",
choices = c("yes", "no")),
conditionalPanel(
condition = paste0("input.field_type_", i, " == 'numeric'"),
selectInput(paste0("range_req_", i), "Are there range restrictions on the input:",
choices = c("no", "yes")),
),
conditionalPanel(
condition = paste0("input.range_req_",i,"== 'yes'"),
numericInput(paste0("min_value_", i), "Minimum Value:", value = NA),
numericInput(paste0("max_value_", i), "Maximum Value:", value = NA)
),
conditionalPanel(
condition = paste0("input.field_type_", i, " == 'string'"),
selectInput(paste0("caped_", i), "Should the input have capitalizations:",
choices = c("no", "yes"))
),
conditionalPanel(
condition = paste0("input.field_type_", i, " == 'string'"),
selectInput(paste0("range_req_string", i), "Are there length restrictions on the input:",
choices = c("no", "yes")),
),
conditionalPanel(
condition = paste0("input.range_req_string",i,"== 'yes'"),
numericInput(paste0("min_value_s", i), "Minimum Value:", value = NA),
numericInput(paste0("max_value_s", i), "Maximum Value:", value = NA)
),
conditionalPanel(
condition = paste0("input.field_type_", i, " == 'options'"),
textInput(paste0("option_input_", i), "Enter the name of the options separated by a comma and no space:")
),
textInput(paste0("error_message_", i), "Enter an error message for your data:"),
)
})
do.call(tagList, field_list)
}
})
output$downloadHighlighted <- downloadHandler(
filename = function() {
paste("highlighted_issues_", Sys.Date(), ".xlsx", sep = "")
},
content = function(file) {
req(input$file)
if (is.null(input$file$datapath) || input$file$datapath == "") {
stop("No file provided. Please upload a dataset before attempting to download.")
}
req(input$study, input$format)
yaml_file_path <- paste0("data_specifications/", input$study, "_", input$format, ".yaml")
if (!file.exists(yaml_file_path)) {
stop("The corresponding YAML specification file does not exist. Please check your study and format selection.")
}
fields <- tryCatch(
yaml::yaml.load_file(yaml_file_path),
error = function(e) stop("Failed to load YAML file. Please ensure the file is valid and accessible.")
)
df <- tryCatch(
read_csv(input$file$datapath),
error = function(e) stop("Failed to read the uploaded dataset. Please ensure the file is in a valid CSV format.")
)
validated <- tryCatch(
validate_dataset(fields, df),
error = function(e) stop("Error during dataset validation: ", e$message)
)
valid <- validated[[1]]
issues <- validated[[2]]
if (!valid) {
wb <- tryCatch(
highlight_csv_to_xlsx(df, issues),
error = function(e) stop("Failed to generate the highlighted workbook: ", e$message)
)
tryCatch(
openxlsx::saveWorkbook(wb, file, overwrite = TRUE),
error = function(e) stop("Failed to save the workbook: ", e$message)
)
} else {
stop("No issues to highlight. The dataset is valid!")
}
}
)
}