-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.R
276 lines (246 loc) · 9.54 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
library(rfm)
library(fable)
library(purrr)
library(furrr)
library(shiny)
library(feasts)
library(scales)
library(future)
library(tsibble)
library(stringr)
library(patchwork)
library(lubridate)
library(tidyverse)
library(shinyalert)
library(data.table)
library(strucchange)
library(future.apply)
library(shinydashboard)
library(shinycssloaders)
library(dashboardthemes)
# English month names in deployment
Sys.setlocale("LC_ALL","C")
plot_font_size <- 20
# For structural breaks
p_value_threshold <- 0.05
source("functions.R")
# source("modeling.R")
# Read RDS instead of running models in Shiny server
models <- readRDS("Data/models.RDS")
segments <- readRDS("Data/segments.RDS")
rfm_result <- readRDS("Data/rfm_results.RDS")
data_to_arima <- readRDS("Data/data_to_arima.RDS")
# Plotting ----------------------------------------------------------------
# Make RFM heat map
rfm_plot <- rfm_heatmap(rfm_result, print_plot = FALSE) +
# Fix missing tiles
geom_rect(aes(xmin = 0.5, xmax = 5.5, ymin = 0.5, ymax = 5.5),
fill = "#F1EEF6") +
geom_tile(aes(frequency_score, recency_score, fill = monetary)) +
ggtitle("Product level RFM") +
theme(text = element_text(colour = "#DAD4D4"),
panel.grid = element_line(colour = "#2D3741"),
panel.background = element_rect(fill = "#2D3741"),
axis.text = element_text(colour = "#BCB1B1", size = plot_font_size),
plot.background = element_rect(fill = "#2D3741", color = "transparent"),
legend.position = "bottom",
legend.key.width = unit(2, "cm"),
legend.box.margin = margin(t = 13),
legend.background = element_rect(fill = "#2D3741"),
legend.text = element_text(size = plot_font_size),
legend.title = element_text(size = plot_font_size),
plot.title = element_text(size = plot_font_size),
axis.title = element_text(size = plot_font_size))
# Segment plot and monetary contribution
rfm_monetary_segments <- segments %>%
group_by(segment) %>%
summarise(median = median(amount)) %>%
arrange(desc(median)) %>%
# Reorder to show product segment hierarchy
mutate(segment = fct_reorder(segment, median)) %>%
ggplot(aes(x = median, y = segment, fill = segment)) +
geom_col() +
scale_fill_brewer(palette = "Blues") +
ggtitle("Median monetary value by product segment") +
xlab("Median monetary value") +
ylab(NULL) +
theme(legend.position = "none",
text = element_text(colour = "#DAD4D4"),
panel.grid = element_line(colour = "#2D3741"),
panel.background = element_blank(),
axis.text = element_text(colour = "#BCB1B1", size = plot_font_size),
plot.background = element_rect(fill = "#2D3741", color = "transparent"),
plot.title = element_text(size = plot_font_size),
axis.title = element_text(size = plot_font_size))
# Shiny components --------------------------------------------------------
ui <- dashboardPage(
title = "Sales dashboard",
dashboardHeader(title = "Price optimization"),
dashboardSidebar(
selectizeInput("segments", "Filter products by segment",
choices = unique(data_to_arima$segment),
multiple = TRUE),
selectizeInput("product_name", "Select product to optimize",
choices = sort(unique(data_to_arima$product_name)),
multiple = FALSE) %>%
tagAppendAttributes(class = "larger"),
actionButton("run_optimization", "Run price optimization"),
hr(),
sidebarMenu(id = "menu",
sidebarMenuOutput("sidebar")
),
tags$head(tags$style(HTML(
# Css to adjust sidebar spacing and coloring
paste('.row {width: 90%;}',
'#product_name+ div>.selectize-input {height: 60px !important;',
'padding-top: 0px !important}',
'#segments+ div>.selectize-input',
'{margin-bottom: 0px; padding-top: 2px !important}',
'.larger {padding-top: 0px !important}',
'#run_optimization {background-color: #00C0EF; color: #FFFFFF}',
'.shiny-bound-input.action-button {margin: auto !important}'
))))
),
dashboardBody(
shinyDashboardThemes(
theme = "grey_dark"
),
useShinyalert(),
# Main content
tabItems(
tabItem(tabName = "rfm",
fluidRow(
column(12,
plotOutput("rfm_plot"),
align = "center"),
tags$head(tags$style(HTML('.row {width: 90%;}'))))
),
tabItem(tabName = "segments",
fluidRow(
column(12,
plotOutput("rfm_monetary_segments"),
align = "center"),
tags$head(tags$style(HTML('.row {width: 90%;}'))))
),
tabItem(tabName = "results",
fluidRow(
column(12,
uiOutput("info_boxes"),
plotOutput(outputId = "combination_plot",
width = "1200px",
height = "700px") %>%
withSpinner(type = 7),
align = "center"),
tags$head(tags$style(HTML(
paste0('.row {width: 90%;}',
'.info-box-content {text-align: left;}')))))
)
)
)
)
server <- function(input, output, session){
# Menu before optimizing without results tab
output$sidebar <- renderMenu({
sidebarMenu(id = "menu",
menuItem("RFM", tabName = "rfm"),
menuItem("Segments", tabName = "segments")
)
})
# Static RFM and RFM segment plots
output$rfm_plot <- renderPlot({
rfm_plot
}, height = 600, width = 750)
output$rfm_monetary_segments <- renderPlot({
rfm_monetary_segments
}, height = 600, width = 750)
# Filter functionality according to RFM segment
observeEvent(input$segments,
updateSelectizeInput(session,
"product_name",
choices = data_to_arima %>%
filter(segment %in% input$segments) %>%
pull(product_name) %>%
unique())
)
# Optimization on button click
observeEvent(input$run_optimization, {
# Optimizing popup
shinyalert("Optimizing...",
type = "info",
closeOnEsc = FALSE,
showConfirmButton = FALSE)
# Update sidebar to include results tab
output$sidebar <- renderMenu({
sidebarMenu(id = "menu",
menuItem("RFM", tabName = "rfm"),
menuItem("Segments", tabName = "segments"),
menuItem("Results", tabName = "results")
)
})
# Make forecasts and get the optimal one
forecasts <- get_forecasts(input$product_name, data_to_arima, models)
optimal_forecast <- get_optimal_forecast(forecasts)
# Boxes with information about the results
output$info_boxes <- renderUI({
fluidRow(
infoBox("Optimized price",
paste0(intToUtf8(163),
number_format(0.01)(
optimal_forecast$new_price)),
"For the next month",
icon("balance-scale")),
infoBox("Optimized revenue",
paste0(intToUtf8(163),
number_format(0.01)(
optimal_forecast$pred_revenue)),
paste0("Up from ",
paste0(intToUtf8(163),
number_format(0.01)(
optimal_forecast$pred_revenue_normal))),
icon("pound-sign")),
infoBox("Revenue increasement",
percent_format(0.1)(optimal_forecast$pred_revenue /
optimal_forecast$pred_revenue_normal - 1)
, "Compared to no optimization",
icon("percent"))
)
})
# Three plots together
output$combination_plot <- renderPlot({
p1 <- isolate(plot_revenue_forecasts(optimal_forecast,
input$product_name,
data_to_arima,
plot_font_size))
p2 <- isolate(plot_quantity_forecasts(optimal_forecast,
input$product_name,
data_to_arima,
plot_font_size))
p3 <- isolate(plot_revenue_price(forecasts,
input$product_name,
data_to_arima,
plot_font_size))
# Plot without warnings
suppressMessages(
print(
((p1 / p2) | p3) +
plot_annotation(
"Effect of price optimization",
theme = theme(
text = element_text(colour = "#DAD4D4"),
plot.background = element_rect(fill = "#2D3741",
color = "transparent"),
plot.title = element_text(size = plot_font_size)))))
})
# Switch tab to results after optimizing
updateTabItems(session, "menu", "results")
# Close optimizing popup
closeAlert()
# Alert if forecasts unavailable
if(length(optimal_forecast$pred_revenue) == 0){
shinyalert(
"Forecasts or optimization could not be done for this product",
type = "error")
}
})
}
shinyApp(ui, server)