-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplumber.R
80 lines (58 loc) · 1.93 KB
/
plumber.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
# R Plumber API to predict marathon time
# load required packages
library(jsonlite)
# load model object and factor levels
load("/opt/ml/marathon_model.RData")
# function to convert HMS to seconds
toSeconds <- function(x){
if (!is.character(x)) stop("x must be a character string of the form H:M:S")
if (length(x)<=0)return(x)
unlist(
lapply(x,
function(i){
i <- as.numeric(strsplit(i,':',fixed=TRUE)[[1]])
if (length(i) == 3)
i[1]*3600 + i[2]*60 + i[3]
else if (length(i) == 2)
i[1]*60 + i[2]
else if (length(i) == 1)
i[1]
}
)
)
}
# function to convert seconds to HMS
toHMS <- function(vec){
# check that numeric vector
if (!is.numeric(vec)) stop("vector must be a numeric")
if (length(vec)<=0) return(vec)
fxn <- function(x) {
H <- formatC(x %/% 3600, width=2, flag="0")
M <- formatC(x %% 3600 %/% 60, width=2, flag="0")
seconds <- x %% 60
# formatting for seconds
if(seconds < 10) {
S <- formatC(x %% 60, width=2, flag="0", digits=1) } else
S <- formatC(x %% 60, width=2, flag="0", digits=2)
paste(H, M, S, sep = ":")
}
# apply to all in vector
lapply(vec, fxn)
}
#' Ping to show server is there
#' @get /ping
function() {
return('')}
#' Handles scoring requests. SageMaker requires it to be a post to /invocations
#' @param req
#' @post /invocations
mpred <- function(req, res) {
# read input data
preddat <- as.data.frame(fromJSON(req$postBody), stringsAsFactors = FALSE)
#preddat <- as.data.frame(fromJSON("example.json"), stringsAsFactors = FALSE)
preddat$hmaratime <- toSeconds(preddat$hmtime)
preddat$gender <- factor(preddat$gender, levels = factor_levels$gender)
m_pred <- predict(lm2_save, newdata = preddat, se.fit = TRUE)
m_out <- as.character(toHMS(m_pred$fit))
return(list(marathon_time = m_out))
}