-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.R
More file actions
63 lines (55 loc) · 1.7 KB
/
Server.R
File metadata and controls
63 lines (55 loc) · 1.7 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
#devtools::install_github('thomasp85/fiery')
#install.packages("routr")
library(fiery)
library(routr)
# Create a New App
app <- Fire$new(port = 10245L)
# Load the libraries it starts
app$on('start', function(server, ...) {
suppressMessages(library(imputeTS))
suppressMessages(library(ggplot2))
suppressMessages(library(tseries))
suppressMessages(library(forecast))
})
router <- RouteStack$new()
route <- Route$new()
router$add_route(route, 'main')
# We start with a catch-all route that provides a welcoming html page
route$add_handler('get', '*', function(request, response, keys, ...) {
response$type <- 'html'
response$status <- 200L
response$body <- '<h1>All your AI are belong to us</h1>'
TRUE
})
# Then on to the /info route
route$add_handler('get', '/info', function(request, response, keys, ...) {
response$status <- 200L
response$body <- structure(R.Version(), class = 'list')
response$format(json = reqres::format_json())
TRUE
})
# Lastly we add the /predict route
route$add_handler('get', '/predict', function(request, response, keys, arg_list, ...) {
response$body <- predict(
arg_list$model,
data.frame(x=as.numeric(request$query$val)),
se.fit = TRUE
)
response$status <- 200L
response$format(json = reqres::format_json())
TRUE
})
# And just to show off reqres file handling, we'll add a route
# for getting a model plot
route$add_handler('get', '/plot', function(request, response, keys, arg_list, ...) {
f_path <- tempfile(fileext = '.png')
png(f_path)
plot(arg_list$model)
dev.off()
response$status <- 200L
response$attach(f_path, filename = 'model_plot.png')
TRUE
})
# Finally we attach the router to the fiery server
app$attach(router)
app$ignite()