-
Notifications
You must be signed in to change notification settings - Fork 1
/
webmice_handlers.R
212 lines (200 loc) · 4.85 KB
/
webmice_handlers.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
#' End point handlers
#'
#' @param .req Required arguments
#' @param .res Result
#' @export
pool_handler <- function(.req, .res) {
pool <- list()
pool$result <- ""
pool$error <- ""
check <- TRUE
json_payload <- as.character(.req$parameters_query[["payload"]])
if (length(json_payload) == 0L) {
check <- FALSE
pool$error <- "No input"
} else {
params <- json_to_parameters(json_payload)
if (is.null(params$data)) {
check <- FALSE
pool$error <- "Error: no data"
}
}
if (!check) {
pool$success <- FALSE
.res$set_body(toJSON(pool))
.res$set_content_type("text/plain")
return()
} else {
print("DEBUG: Calling pool.table (requires 3.16.4)")
poolres <- call_pool(params$data)
if (is.null(poolres$error)) {
pool$success <- TRUE
pool$result <- poolres
} else {
pool$success <- FALSE
pool$error <- poolres$error
}
}
.res$set_body(toJSON(pool, force = TRUE))
.res$set_content_type("text/plain")
}
#' @rdname fit_handler
#'
#' @param .req Required arguments
#' @param .res Result
#' @export
fit_handler <- function(.req, .res) {
fit <- list()
fit$result <- ""
fit$error <- ""
check <- TRUE
json_payload <- as.character(.req$parameters_query[["payload"]])
# if answers are copied straight from the Swagger interface,
# there are too many backslashes
json_payload <- gsub("\\\\", "", json_payload)
if (length(json_payload) == 0L) {
check <- FALSE
fit$error <- "No input"
} else {
params <- json_to_parameters(json_payload)
if (is.null(params$data)) {
check <- FALSE
fit$error <- "No data"
}
if (is.null(params$model)) {
check <- FALSE
fit$error <- "No model"
}
if (is.null(params$formula)) {
check <- FALSE
fit$error <- "No formula"
}
}
if (!check) {
fit$success <- FALSE
.res$set_body(toJSON(fit))
.res$set_content_type("text/plain")
return()
} else {
fitres <- call_with(params$data, params$model, params$formula)
if (is.null(fitres$error)) {
fit$success <- TRUE
fit$result <- fitres
} else {
fit$success <- FALSE
fit$error <- fitres$error
}
}
.res$set_body(toJSON(fit, force = TRUE))
.res$set_content_type("text/plain")
}
#' @rdname impute_longfmt_handler
#'
#' @param .req Required arguments
#' @param .res Result
#' @export
impute_longfmt_handler <- function(.req, .res) {
json_payload <- as.character(.req$parameters_query[["payload"]])
impute <- list()
check <- TRUE
if (length(json_payload) == 0L) {
check <- FALSE
impute$error <- "No input"
} else {
# check if convertible to json
params <- json_to_parameters(json_payload)
if (is.null(params)) {
check <- FALSE
impute$error <- "Input format error, not a json"
} else {
# impute function needs data
if (is.null(params$data)) {
check <- FALSE
impute$error <- "No data"
}
if (is.null(params$maxit)) {
check <- FALSE
impute$error <- "No maxit"
}
if (is.null(params$seed)) {
check <- FALSE
impute$error <- "No seed"
}
}
}
if (!check) {
impute$success <- FALSE
.res$set_body(toJSON(impute))
.res$set_content_type("text/plain")
return()
} else {
imp <- call_mice(params)
if (is.null(imp$error)) {
impute$success <- TRUE
impute$result <- imp_result_long_fmt(imp)
} else {
impute$success <- FALSE
impute$error <- imp$error
}
.res$set_body(toJSON(impute, force = TRUE))
.res$set_content_type("text/plain")
}
}
#' @rdname example_data_handler
#'
#' @param .req Required arguments
#' @param .res Result
#' @export
example_data_handler <- function(.req, .res) {
data <- list()
data$success <- ""
data$result <- ""
data$error <- ""
example_name <- as.character(.req$parameters_query[["name"]])
result <- tryCatch(
{
data$result <- get(example_name)
data$success <- TRUE
},
error = function(e) {
res <- list()
res$success <- FALSE
res$result <- ""
res$error <- as.character(e)
return(res)
}
)
if (typeof(result) == "list") {
data <- result
}
.res$set_body(toJSON(data))
.res$set_content_type("text/plain")
}
#' @rdname mice_version_handler
#'
#' @param .req Required arguments
#' @param .res Result
#' @export#' @export
mice_version_handler <- function(.req, .res) {
version <- list()
version$success <- ""
version$error <- ""
result <- tryCatch(
{
version$result <- sessionInfo("mice")$otherPkgs$mice$Version
version$success <- TRUE
},
warning = function(w) {
res <- list()
res$success <- FALSE
res$result <- ""
res$error <- w$message
return(res)
}
)
if (typeof(result) == "list") {
version <- result
}
.res$set_body(toJSON(version))
.res$set_content_type("text/plain")
}