-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.R
134 lines (121 loc) · 4.07 KB
/
server.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
library(shiny)
library(surveillance)
library(ggplot2)
library(plotly)
library(dplyr)
library(purrr)
library(viridis)
source("global.R")
sts_to_df <- function(algorithm, sts) {
data.frame(
algorithm,
epoch = epoch(sts, as.Date = TRUE),
observed = as.numeric(observed(sts)),
alarm = as.logical(alarms(sts)),
state = as.logical(sts@state),
upperbound = as.numeric(upperbound(sts))
)
}
shinyServer(function(input, output) {
algo_range <- reactive({
min_range <- pmax(0, nrow(time_series()) - as.numeric(input$range_min))
min_range:nrow(time_series())
})
algorithms <- reactiveValues()
log <- reactiveValues()
output$errors <- renderText({
if (!is.null(log[["error"]])) {
log[["error"]]
}
})
# ears
observeEvent({input$update_algos; time_series()}, {
result <- purrr::safely(~surveillance::earsC(time_series(), control = list(alpha = input$ears_alpha,
method = input$ears_method,
range = algo_range())))()
if (!is.null(result$result)) {
algorithms[["ears"]] <- result$result
} else {
algorithms[["ears"]] <- time_series()[1, ]
log[["error"]] <- result$error$message
log[["error"]] <- paste("EARSC:", result$error$message)
}
})
# farringtonflexible
observeEvent({input$update_algos; time_series()}, {
result <- purrr::safely(~surveillance::farringtonFlexible(time_series(), control = list(
alpha = input$farringtonflexible_alpha,
b = input$farringtonflexible_b,
w = input$farringtonflexible_w,
pastWeeksNotIncluded = input$farringtonflexible_pastWeeksNotIncluded,
powertrans = input$farringtonflexible_powertrans,
range = algo_range()
)))()
if (!is.null(result$result)) {
algorithms[["farringtonflexible"]] <- result$result
} else {
algorithms[["farringtonflexible"]] <- time_series()[1, ]
log[["error"]] <- paste("farringtonflexible:", result$error$message)
}
})
# glrnb
observeEvent({input$update_algos; time_series()}, {
result <- purrr::safely(~surveillance::glrnb(time_series(), control = list(
"c.ARL" = input$glrnb_c_ARL,
ret = "cases",
theta = 1 + input$glrnb_theta / 100,
mu0 = list(S = input$glrnb_S, trend = input$glrnb_trend),
range = algo_range()
)))()
if (!is.null(result$result)) {
algorithms[["glrnb"]] <- result$result
} else {
algorithms[["glrnb"]] <- time_series()[1, ]
log[["error"]] <- paste("glrnb:", result$error$message)
}
})
# data
time_series <- reactive({
ds_env <- new.env()
data(list = input$dataset, envir = ds_env)
dataset <- as.list(ds_env)[[input$dataset]]
if (class(dataset) == "disProg") {
surveillance::disProg2sts(dataset)
} else {
dataset
}
})
surv_ts <- reactive({
base_data <- sts_to_df("tmp", time_series()) %>%
distinct(epoch, observed)
purrr::map(input$algorithms, function(algo) {
base_data %>% left_join(sts_to_df(algo, algorithms[[algo]]),
by = c("epoch", "observed")) %>%
mutate(algorithm = algorithm_name_dict[algo])
}) %>% bind_rows
})
output$mainPlot <- plotly::renderPlotly({
limit_data <- function(data) {
if (input$dataset_last_weeks > 0 &&
input$dataset_last_weeks <= nrow(data)) {
tail(dplyr::arrange(data, epoch), n = input$dataset_last_weeks)
} else {
data
}
}
plot_data <- limit_data(surv_ts())
alarm_data <- plot_data %>%
filter(alarm)
outbreak_data <- plot_data %>%
filter(state == TRUE)
plotly::ggplotly(ggplot(plot_data, aes(x = epoch, y = observed)) +
geom_bar(stat = "identity", color = "#123456") +
geom_line(aes(y = upperbound), color = "orange") +
facet_wrap(~ algorithm) +
xlab("ISO week") +
ylab("# cases") +
#geom_point(data = outbreak_data, na.rm = TRUE, y = 0, color = "black", shape = "cross") +
geom_point(data = alarm_data, color = "red", na.rm = TRUE, y = 0)
)
})
})