-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
397 lines (340 loc) · 18.9 KB
/
app.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# load libraries
library(DT)
library(shiny)
library(shinydashboard)
library(shinyjs)
library(dplyr)
library(prophet)
library(ggplot2)www
library(anytime)
library(lubridate)
ui <- dashboardPage(
dashboardHeader(title = "Storage Forecaster"),
## Sidebar ------------------------------------
dashboardSidebar(
sidebarMenu(
menuItem("About", tabName = "About"),
menuItem("Storage Forecaster", tabName = "Prophet")
)
),
## Body ------------------------------------
dashboardBody(
### include css file --------------------
tags$head(tags$style(includeCSS("./www/mycss.css"))),
### include script with function openTab----------------------
tags$script(HTML("var openTab = function(tabName){$('a', $('.sidebar')).each(function() {
if(this.getAttribute('data-value') == tabName) {
this.click()
};
});
}
")),
### use shinyjs -----------------------
useShinyjs(),
## Tab Items ---------------------------
tabItems(
### ABout ----------------------------
tabItem(tabName = "About",
fluidRow(
box(width=12,
infoBox(width = 12,
title = "",
value = includeHTML("./www/about.html"),
icon = icon("info")),
column(width = 3,
a(actionButton(inputId = "start",
label = "Get Started",
style = "font-size: 150%'"),
onclick = "openTab('Prophet')",
style="cursor: pointer; font-size: 300%;")))
)
),
### Prophet ----------------------------
tabItem(tabName = "Prophet",
fluidRow(
box(width = 12,
tabsetPanel(id = "inTabset",
## TAB 1 : Upload Data --------------------------
tabPanel(title = "Upload Data", value = "panel1",
fluidRow(
## upload main dataset -----------------
column(width = 6,
# column(width = 12,
tags$h4("Main Dataset"),
helpText("A delimited fsimage file generated by HDFS OIV"),
fileInput("ts_file","Upload tab delimited fsimage file"),
conditionalPanel(condition = 'output.panelStatus',
helpText("First 6 rows of the uploaded data")),
tableOutput("uploaded_data"),
### error msg if main dataset is not valid
uiOutput("msg_main_data")
)
),
## Next 1 ---------------
fluidRow(
column(width = 2, offset = 10,
shinyjs::disabled(actionButton("next1", "Next",
style = "width:100%; font-size:200%"))))
),
## TAB 2 : Set Parameters -----------------------------------
tabPanel(title = "Set Parameters", value = "panel2",
fluidRow(
column(width = 8,
column(width = 8, offset = 2,
tags$h3("Prophet Parameters")),
column(width = 6,
radioButtons("growth","growth",
c('linear'), inline = TRUE),
### parameter: yearly.seasonality
checkboxInput("yearly","yearly.seasonality", value = TRUE),
### parameter: weekly.seasonality
checkboxInput("monthly","weekly.seasonality", value = TRUE),
### parameter: n.changepoints
numericInput("n.changepoints","n.changepoints", value = 25),
### parameter: seasonality.prior.scale
numericInput("seasonality_scale","seasonality.prior.scale", value = 10),
### parameter: changepoint.prior.scale
numericInput("changepoint_scale","changepoint.prior.scale", value = 0.05, step = 0.01)),
column(width = 6,
### parameter: mcmc.samples
numericInput("mcmc.samples", "mcmc.samples", value = 0),
### parameter: interval.width
numericInput("interval.width", "interval.width", value= 0.8, step = 0.1),
### parameter: uncertainty.samples
numericInput("uncertainty.samples","uncertainty.samples", value = 1000))
),
## predict parameters --------------------
column(width = 4,
tags$h3("Predict Parameters"),
### paramater: periods
numericInput("periods","periods",value=365),
### parameter: freq
selectInput("freq","freq",
choices = c('day', 'week', 'month', 'quarter','year')),
### parameter: include_history
checkboxInput("include_history","include_history", value = TRUE)
)
)
,
## Back/Next 2 --------------------------
fluidRow(
column(width = 2, offset = 10,
actionButton("next2", "Next",
style = "width:100%; font-size:200%"))
)
),
## TAB 3 : Fit Propher Model ----------------------
tabPanel(title = "Fit Model", value = "panel3",
fluidRow(
# box(width = 12,
column(width = 12,
shinyjs::disabled(actionButton("plot_btn2", "Fit Prophet Model",
style = "width:30%; margin-top: 25px; margin-bottom: 50px; font-size:150%; ")
)
)
),
## Results Box : collapsible ------------------
fluidRow(
conditionalPanel("input.plot_btn2",
box(width = 12, collapsible = T, title = "Results",
div(id = "output-container3",
tags$img(src = "spinner.gif",
id = "loading-spinner"),
DT::dataTableOutput("data")),
conditionalPanel("output.data",
uiOutput("dw_button")
)
)
)),
## Plots Box : collapsible ------------------
fluidRow(
conditionalPanel("input.plot_btn2",
box(width = 12, collapsible = T, title = "Plots",
tabsetPanel(
tabPanel("Forecast Plot",
div(id = "output-container",
# tags$img(src = "spinner.gif",
# id = "loading-spinner"),
plotOutput("ts_plot")
)
# )
),
tabPanel("Prophet Plot Components",
# output.logistic_check=='no_error'
conditionalPanel("input.plot_btn2",
div(id = "output-container",
# tags$img(src = "spinner.gif",
# id = "loading-spinner"),
plotOutput("prophet_comp_plot"))
)
)
)))),
## back 3 ------------
fluidRow(
column(width = 2,
actionButton("back3", "Back",
style = "width:100%; font-size:200%"))
)
)
)
)
)))
)
)
server <- function(input, output, session) {
addClass(selector = "body", class = "sidebar-collapse")
## Next/Back Buttons actions (to be turned into modules)---------------------------
observeEvent(input$next1, {
updateTabsetPanel(session, "inTabset",
selected = "panel2")
})
observeEvent(input$next2, {
updateTabsetPanel(session, "inTabset",
selected = "panel3")
})
observeEvent(input$back2, {
updateTabsetPanel(session, "inTabset",
selected = "panel1")
})
observeEvent(input$back3, {
updateTabsetPanel(session, "inTabset",
selected = "panel2")
})
observeEvent(input$back4, {
updateTabsetPanel(session, "inTabset",
selected = "panel3")
})
## function: duplicatedReactive values -----------------------------
duplicatedRecative <- function(signal){
values <- reactiveValues(val="")
observe({
values$val <- signal()
})
reactive(values$val)
}
## read out file main data------------------------------------------------
dat <- reactive({
req(input$ts_file)
file_in <- input$ts_file
fsimage <- read.table(file_in$datapath, sep="\t", header = T) # read csv
files <- fsimage %>%
filter(FileSize > 0) %>%
select (FileSize, Replication, ModificationTime)
files_used <- mutate(files, RawSize = (((FileSize/1024)/1024)/1024) * Replication, MTime = anytime(ModificationTime)) %>%
select (RawSize, MTime) %>%
group_by(day=floor_date(MTime, "day")) %>%
summarize(RawSize = sum(RawSize))
names(files_used)[1] <- "ds"
names(files_used)[2] <- "y"
return(files_used)
})
## Toggle submit button state according to main data -----------------------
observe({
if(!(c("ds","y") %in% names(dat()) %>% mean ==1))
shinyjs::disable("next1")
else if(c("ds","y") %in% names(dat()) %>% mean ==1)
shinyjs::enable("next1")
})
## output: table of 1st 6 rows of uploaded main data ------------------
output$uploaded_data <- renderTable({
req(dat)
head(dat())
})
## panel status depending on main data ------------------------
output$panelStatus <- reactive({
nrow(dat())>0
})
outputOptions(output, "panelStatus", suspendWhenHidden = FALSE)
## Toggle submit button state according to data ---------------
observe({
if(!(c("ds","y") %in% names(dat()) %>% mean ==1))
shinyjs::disable("plot_btn2")
else if(c("ds","y") %in% names(dat()) %>% mean ==1)
shinyjs::enable("plot_btn2")
})
## create prophet model --------------------------------------------------
prophet_model <- eventReactive(input$plot_btn2,{
req(dat(),
# ("ds" %in% dat()), "y" %in% names(dat()),
input$n.changepoints,
input$seasonality_scale, input$changepoint_scale,
input$mcmc.samples,
input$mcmc.samples, input$interval.width,
input$uncertainty.samples)
if(input$growth == "logistic"){
validate(
need(try("cap" %in% names(dat())),
"Error: for logistic 'growth', the input dataframe must have a column 'cap' that specifies the capacity at each 'ds'."))
}
kk <- prophet(dat(),
growth = input$growth,
changepoints = NULL,
n.changepoints = input$n.changepoints,
yearly.seasonality = input$yearly,
weekly.seasonality = input$monthly,
seasonality.prior.scale = input$seasonality_scale,
changepoint.prior.scale = input$changepoint_scale,
mcmc.samples = input$mcmc.samples,
interval.width = input$interval.width,
uncertainty.samples = input$uncertainty.samples,
fit = T)
return(kk)
})
## dup reactive prophet_model ------------------------------
p_model <- duplicatedRecative(prophet_model)
## Make dataframe with future dates for forecasting -------------
future <- eventReactive(input$plot_btn2,{
req(p_model(),input$periods, input$freq)
make_future_dataframe(p_model(),
periods = input$periods,
freq = input$freq,
include_history = input$include_history)
})
## dup reactive future--------------------------
p_future <- duplicatedRecative(future)
## predict future values -----------------------
forecast <- reactive({
req(prophet_model(),p_future())
predict(prophet_model(),p_future())
})
## dup reactive forecast--------------------------
p_forecast <- duplicatedRecative(forecast)
## output : datatable from forecast dataframe --------------------
output$data <- renderDataTable({
# req(logistic_check()!="error")
DT::datatable(forecast(),
options = list(scrollX = TRUE, pageLength = 5)) %>%
formatRound(columns=2:17,digits=4)
})
## download button ----------------
output$dw_button <- renderUI({
req(forecast())
downloadButton('downloadData', 'Download Data',
style = "width:20%;
margin-bottom: 25px;
margin-top: 25px;")
})
output$downloadData <- downloadHandler(
filename = "forecast_data.csv",
content = function(file) {
write.csv(forecast(), file)
}
)
## output: plot forecast -------------
output$ts_plot <- renderPlot({
# req(logistic_check()!="error")
g <- plot(p_model(), forecast())
g+theme_classic()
})
## output:plot prophet components --------------
output$prophet_comp_plot <- renderPlot({
# req(logistic_check()!="error")
prophet_plot_components(p_model(), forecast())
})
## error msg for main dataset------------------------
output$msg_main_data <- renderUI({
if(c("ds","y") %in% names(dat()) %>% mean !=1)
"Invalid Input: dataframe should have at least two columns named (ds & y)"
})
}
shinyApp(ui, server)