-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
245 lines (148 loc) · 8.78 KB
/
app.R
File metadata and controls
245 lines (148 loc) · 8.78 KB
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
# Packages ----------------------------------------------------------------
options( scipen = 999 )
library( shiny )
library( data.table )
library( ggplot2 )
library( tidyr )
BFsales <- fread( "BlackFriday.csv" )
shinyApp(
# UI ----------------------------------------------------------------------
ui = fluidPage( title = "Black Friday",
titlePanel( tagList( icon( "shopping-basket" ), "Black Friday sales" ) ),
br(),
sidebarLayout(
sidebarPanel( width = 3,
checkboxGroupInput( inputId = "Gender",
label = "Select customer gender:",
choices = c( "Male", "Female" ),
selected = c( "Male", "Female" ) ),
br(),
sliderInput( inputId = "YearsSpentInCity",
label = "Number of years spent in city:",
min = 0, max = 4,
value = c( 0, 4 ) ),
br(),
radioButtons(
inputId = "splitByProdCat",
label = "Subset by product category?",
choices = c( "No" = "No",
"Yes" = "Yes" ),
selected = "No" ),
br(),
conditionalPanel(
condition = "input.splitByProdCat == 'Yes'",
uiOutput( "productCategories" ) # show all product categories
)
),
mainPanel ( width = 9,
h2( "Line graph" ),
plotOutput( outputId = "linePlot", width = 750 ),
br(),
fluidRow(
column( width = 9,
column( width = 6,
h3( "Average spend" ),
tableOutput( "dataDescr" ) ),
column( width = 6,
h3( "Sample size" ),
br(),
verbatimTextOutput( "info" ) ) )
)
)
)
),
# Server ------------------------------------------------------------------
server = function( input, output ) {
prepData <- reactive({
# print( "PREPPING DATA ON START" )
BFsales[ , User_ID := as.factor( User_ID ) ]
BFsales[ , Product_ID := as.factor( Product_ID ) ]
BFsales[ , Occupation := as.factor( Occupation ) ]
BFsales[ , Gender := as.factor( Gender ) ]
levels( BFsales$Gender ) <- c( "Female", "Male" )
BFsales[ , Stay_In_Current_City_Years := ifelse( Stay_In_Current_City_Years == "4+", "4", Stay_In_Current_City_Years ) ]
BFsales[ , Stay_In_Current_City_Years := ordered( Stay_In_Current_City_Years,
levels = sort( unique( Stay_In_Current_City_Years ) ) ) ]
BFsales[ , Marital_Status := factor( Marital_Status ) ]
levels( BFsales$Marital_Status ) <- c( "Married", "Single" )
BFsales[ , Product_Category_1 := as.factor( Product_Category_1 ) ]
BFsales[ , Product_Category_2 := as.factor( Product_Category_2 ) ]
BFsales[ , Product_Category_3 := as.factor( Product_Category_3 ) ]
BFsales[ , Age := ifelse( Age == "0-17", "Under 17", Age ) ]
BFsales[ , Age := ifelse( Age == "55+", "Over 55", Age ) ]
BFsales[ , Age := ordered( Age, levels = c( "Under 17", "18-25", "26-35", "36-45", "46-50", "51-55", "Over 55" ) ) ]
return( BFsales )
})
output$productCategories <- renderUI({
selectInput( inputId = "prodCats",
label = "Select product category: ",
choices = levels( prepData()$Product_Category_1 ),
selected = min( as.numeric( prepData()$Product_Category_1 ) ) )
})
subsetData <- reactive({
# print( "GENERATING DATA SUBSET BASED ON INPUTS" )
if ( input$splitByProdCat == 'No' ) {
selected_subset <- prepData()
}
else if ( input$splitByProdCat == 'Yes' ) {
validate( need( ! is.null( input$prodCats ), "Please wait. Generating dynamic menu from data..." ) )
selected_subset <- prepData()[ Product_Category_1 == input$prodCats, ]
}
selected_subset <- selected_subset[ Gender %in% input$Gender, ]
selected_subset <- selected_subset[ input$YearsSpentInCity[ 1 ] <= Stay_In_Current_City_Years &
Stay_In_Current_City_Years <= input$YearsSpentInCity[ 2 ], ]
return( selected_subset )
})
getAverageSpend <- reactive({
purchase_agr <- aggregate( Purchase ~ User_ID + Age + City_Category, data = subsetData(), FUN = sum )
purchase_agr <- aggregate( Purchase ~ Age + City_Category, data = purchase_agr, FUN = mean )
return( purchase_agr )
})
output$dataDescr <- renderTable({
# print( "CREATING TABLE OF SUBSET" )
tabular_vals <- dcast( Age ~ City_Category, value.var = "Purchase", data = getAverageSpend() )
setDT( tabular_vals )
tabular_vals[ , A := format( A, nsmall = 2, big.mark = "," ) ]
tabular_vals[ , B := format( B, nsmall = 2, big.mark = "," ) ]
tabular_vals[ , C := format( C, nsmall = 2, big.mark = "," ) ]
setnames( tabular_vals, c( "Age band", "City type A", "City type B", "City type C" ) )
return( tabular_vals )
}, align = "r" )
output$linePlot <- renderPlot({
# print( "DRAWING PLOT" )
ggplot( getAverageSpend(),
aes( x = City_Category, y = Purchase, group = Age, color = Age ) ) +
geom_point( size = 2.5 ) +
geom_line( lwd = 1.5 ) +
scale_color_viridis_d( direction = -1, begin = 0.20, end = 0.85, option = "B" ) +
labs( x = "City category",
color = "Age band" ) +
ggtitle( "Average spend according to customer age and city type",
subtitle = "Total spend computed per customer, and then averaged across age bands and locations" ) +
theme( text = element_text( size = 16 ) )
})
output$info <- renderText({
# print( "GETTING SAMPLE SIZE" )
sample_size_per_city_type <- aggregate( User_ID ~ City_Category, data = subsetData(), FUN = function( x ) length( unique( x ) ) )
A <- sample_size_per_city_type[[ 2 ]][ 1 ]
B <- sample_size_per_city_type[[ 2 ]][ 2 ]
C <- sample_size_per_city_type[[ 2 ]][ 3 ]
paste( "Total number of customers for each city type\n(under input conditions defined): \n",
"City type A =", format( A, big.mark = "," ), "\n",
"City type B =", format( B, big.mark = "," ), "\n",
"City type C =", format( C, big.mark = "," ) )
})
}
)
# Check out the deployed app: -----------------------------------------------------------------
# https://thedatalab.shinyapps.io/BlackFridayShinyApp/
# Discover more -----------------------------------------------------------
# https://deanattali.com/blog/building-shiny-apps-tutorial/
# http://rstudio.github.io/shiny/tutorial/
# https://shiny.rstudio.com/articles
# Homework ----------------------------------------------------------------
# Tweak the UI to include suitable inputs for:
## Occupation
## Marital status
## Product category 2 or 3 (careful about handling missing data here)
# And then update the server function to use these newly-added inputs!