-
Notifications
You must be signed in to change notification settings - Fork 0
/
plumber.R
77 lines (63 loc) · 1.92 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
# Routing for pangoRo package functionality
library(plumber)
library(pangoRo)
# Create global object to share (avoid many pulls)
pangoro_obj <- pangoro(refresh = TRUE, offline = FALSE)
# For refresh purposes (global)
time_stamp <- Sys.time()
date_stamp <- format(time_stamp, '%Y-%m-%d')
#* @apiTitle Plumber API for {pangoRo}
#* Search pangoro
#* @post /search
#* @parser json list(simplifyVector = TRUE)
#* @serializer unboxedJSON
function(req) {
if(length(req$body$direction) > 1) stop('Vector must be of length 1 for direction parameter')
check_input_size(req$body$input)
check_input_size(req$body$search)
refresh_pangoro(date_stamp)
mapply(req$body$input, req$body$search, req$body$direction,
FUN = \(x, y, z) search_pangoro(pangoro_obj, x, y, z))
}
#* Expand pangoro
#* @post /expand
#* @parser json list(simplifyVector = TRUE)
#* @serializer unboxedJSON
function(req) {
check_input_size(req$body)
refresh_pangoro(date_stamp)
pangoro_obj |>
expand_pangoro(req$body)
}
#* Collapse pangoro
#* @post /collapse
#* @parser json list(simplifyVector = TRUE)
#* @serializer unboxedJSON
function(req) {
check_input_size(req$body)
refresh_pangoro(date_stamp)
pangoro_obj |>
collapse_pangoro(req$body)
}
#* Refresh pangoro object
#* @put /refresh
function() {
refresh_pangoro(date_stamp = date_stamp)
}
#* Retrieve latest time refreshed
#* @get /latest
function() {
date_stamp
}
# Function to detect timing of last refresh of pangoro object
refresh_pangoro <- function(date_stamp, date_span = 1, envir = .GlobalEnv) {
curr_date <- Sys.Date()
if( difftime(curr_date, date_stamp, units = 'days') >= date_span) {
assign("pangoro_obj", pangoRo::pangoro(refresh = TRUE), envir = envir)
assign("date_stamp", curr_date, envir = envir)
}
}
# Function for input length control
check_input_size <- function(input, threshold = 1e5) {
if(length(input) > threshold) stop('Input provided is too large.')
}