-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeSeriesPlot.R
146 lines (134 loc) · 5.39 KB
/
TimeSeriesPlot.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
# time serice visualization:
library(plotly)
library(dplyr)
library(shiny)
library(RColorBrewer)
library(shinycssloaders)
# install.packages("remotes")
# remotes::install_github("AnalytixWare/ShinySky")
# library(shinysky)
library(shinyWidgets)
f_labels <- function(x) {
switch(x,
"Cases" = return("New cases"),
"cum_cases" = return("Cumulative confirmed cases"),
"Deaths" = return("New deaths"),
"cum_death" = return("Cumulative deaths"),
"Recovered" = return("New recovereis"),
"cum_recovered" = return("Cumulative recoveries")
)
}
ui_timeSeriesPlot <- function(id) {
ns <- NS(id)
tagList(
selectInput(ns("inp"), label = "Add/Remove regions", choices = c(1, 2), multiple = TRUE),
flowLayout(
prettyCheckbox(ns("help"), label = "Hint", value = FALSE),
uiOutput(ns("log2show"))
# uiOutput(ns("regression2show"))
),
conditionalPanel("input.help",
ns = ns,
HTML("<p style = 'color:blue;font-size = 30px;font-weight:bold'>
You can add different regions to the graph by typing the name of the region in the search bar above and then pressing enter. Remove a region by clicking on the its name and pressing the backspace key. If you click on a specific curve, more statistical details appear below for the corresponding region.
</p>")
),
withSpinner(uiOutput(ns("uiplt")), type = 4),
uiOutput(ns("pl"))
)
}
server_timeSeriesPlot <- function(input, output, session, data, var2show, source = NULL, showLABEL = TRUE, n = NULL, showLog = FALSE) {
if (is.null(n)) n <- 1
if (is.null(source)) source <- "A"
ns <- session$ns
co <- reactive({
switch(var2show,
"Cases" = ntop_data(data, n, "case"),
"Deaths" = ntop_data(data, n, "death"),
"Recovered" = ntop_data(data, n, "recovered"),
"cum_cases" = ntop_data(data, n, "case"),
"cum_death" = ntop_data(data, n, "death"),
"cum_recovered" = ntop_data(data, n, "recovered"),
"date_case" = ntop_data(data, n, "date_case"),
"date_death" = ntop_data(data, n, "date_death")
)
})
observe({
updateSelectInput(session = session, inputId = "inp", choices = unique(data$Province), selected = co()$Province)
})
province <- reactive(input$inp)
new_data1 <- reactive({
data %>%
filter(Province %in% province())
})
output$log2show <- renderUI({
if (stringr::str_starts(var2show, "cum")) {
prettyCheckbox(inputId = ns("Log"), label = "Logarithmic scale", value = FALSE)
}
})
output$uiplt <- renderUI(
tagList(
conditionalPanel("!input.regression",
plotlyOutput(ns("plt")),
ns = ns
),
# conditionalPanel("input.regression",
# flowLayout(
# numericInput(ns("upper"), "Number of days to be forecasted", min = 1, max = 100, value = 60),
# numericInput(ns("step"), "Step of the x axis", min = 1, max = 50, value = 10),
# prettyCheckbox(ns("conf"), "Add 95% confidence interval", value = FALSE),
# radioGroupButtons(ns("modell"), choices = list(
# "Logistic" = TRUE,
# "Gompertz" = FALSE
# ), selected = TRUE)
# ),
# plotlyOutput(ns("regplt")),
# ns = ns
# )
)
)
output$plt <- renderPlotly({
# browser()
new_data1() %>%
select(DateRep, var2show, Province) %>%
mutate(Province = factor(Province, levels = unique(Province))) %>%
plot_ly(
x = ~DateRep, y = ~ get(var2show), color = ~Province,
# x = ~DateRep, y = ~ var2show, color = ~Province,
colors = "Dark2", no.white = TRUE, steps = 2, customdata = ~Province, source = source
# colors = "Dark2", no.white = FALSE, steps = 2, source = source
) %>%
add_lines(marker = list(size = 6)) %>%
layout(yaxis = list(title = f_labels(var2show), type = ifelse(input$Log, "log", "linear")), xaxis = list(title = "Date reported"))
})
new_data2 <- reactiveVal()
observeEvent(event_data("plotly_click", source = source), {
df <- data %>%
filter(Province %in% event_data("plotly_click", source = source)$customdata)
new_data2(df)
})
output$pl <- renderUI(
ui_province_property(ns("f_1"))
)
observeEvent(event_data("plotly_click", source = source), callModule(server_province_property, "f_1", new_data2, var2show, showLABEL = showLABEL, Log = input$Log))
#-----------------------
# regression:
#-----------------------
# output$regression2show <- renderUI({
# if (stringr::str_starts(var2show, "cum") && showLog) {
# prettyCheckbox(
# ns("regression"),
# label = div(
# style = "font-size: 16px;color:purple;font-weight:bold",
# "Prediction of the future"
# ),
# value = FALSE
# )
# }
# })
#
# output$regplt <- renderPlotly({
# LGM_plot(data = data, Region_name = province(), upper = input$upper, conf = input$conf, Step = input$step, Log = input$Log, modell = input$modell)
# # LGM(new_data1()$cum_cases, new_data1()$DateRep, unique(new_data1()$Province), upper = input$upper, conf = input$conf, Step = input$step,Log = input$Log)
# })
}