This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.R
320 lines (273 loc) · 13.5 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
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
shinyServer(function(input, output) {
# =========================================================================
# Reactive resources
# =========================================================================
resource.studios <- reactive({
df <- dataframes$studios %>% # subset/filter df_base based on user selections
filter(YEAR >= input$studios_years[1],
YEAR <= input$studios_years[2])
return(df)
})
resource.oscars <- reactive({
df <- dataframes$oscars %>% # subset/filter df_base based on user selections
filter(YEAR >= input$oscars_years[1],
YEAR <= input$oscars_years[2])
return(df)
})
resource.actors <- reactive({
df <- dataframes$actors
return(df)
})
resource.directors <- reactive({
df <- dataframes$directors
return(df)
})
resource.producers <- reactive({
df <- dataframes$producers
return(df)
})
# =========================================================================
# Server outputs : Datatables
# =========================================================================
output$studios_datatable <- renderDataTable({
return(resource.studios())
}, options=list(pageLength=10, autoWidth=FALSE))
output$oscars_datable <- renderDataTable({
return(resource.oscars())
}, options=list(pageLength=10, autoWidth=FALSE))
output$actors_datatable <- renderDataTable({
return(resource.actors())
}, options=list(pageLength=10, autoWidth=FALSE))
output$directors_datatable <- renderDataTable({
return(resource.directors())
}, options=list(pageLength=10, autoWidth=FALSE))
output$producers_datatable <- renderDataTable({
return(resource.producers())
}, options=list(pageLength=10, autoWidth=FALSE))
# =========================================================================
# Server outputs : Plots
# =========================================================================
output$studios_box_office_timeseries <- renderPlot({
# get data from dataframe
df <- resource.studios() %>%
arrange(-YEAR, RANK) %>% # order by rank
mutate(STUDIO = factor(STUDIO, levels=unique(STUDIO))) # convert to factor for fixing plot sort ordering
# Build colormap
studios <- unique(df$STUDIO) # get list of unique studios in filtered dataframe
colormap <- helper.colormapper(studios, c(input$studios_studio1, input$studios_studio2, input$studios_studio3))
# plotting
plot <- ggplot(df, aes(x=YEAR, y=BOXOFFICE, group=STUDIO, color=STUDIO)) +
geom_point(size=3) +
geom_line(size=1.2, alpha=0.5) +
geom_text(data=subset(df, YEAR == input$studios_years_max), # hack to label only at end of visualization
aes(x=YEAR, y=BOXOFFICE, label=STUDIO),
size=5,
hjust=1) +
scale_color_manual(values=colormap) +
scale_x_continuous(breaks=seq(min(df$YEAR), max(df$YEAR), by=1)) +
scale_y_continuous(labels=dollar) +
labs(title="Annual Top 10 Studio Rankings by Box Office",
x="Year",
y="Box Office ($M)") +
theme(panel.background = element_blank())
return(plot)
})
output$studios_movies_facet <- renderPlot({
# get data from dataframe
df <- resource.studios() %>%
arrange(-YEAR, -MOVIES_COUNT) %>% # order by most movies_count
mutate(STUDIO = factor(STUDIO, levels=unique(STUDIO))) # convert to factor for fixing plot sort ordering
# Build colormap
studios <- unique(df$STUDIO) # get list of unique studios in filtered dataframe
colormap <- helper.colormapper(studios, c(input$studios_studio1, input$studios_studio2, input$studios_studio3))
# plotting
plot <- ggplot(df, aes(x=YEAR, y=MOVIES_COUNT, group=STUDIO, fill=STUDIO)) +
facet_wrap(~ STUDIO, nrow=4) +
geom_bar(stat="identity") +
scale_fill_manual(values=colormap) +
scale_x_continuous(breaks=as.integer(seq(min(df$YEAR), max(df$YEAR), length.out=3))) +
labs(title="Annual Top 10 Studio Rankings by Movies Produced",
x="Year",
y="Movies Produced") +
theme(panel.background = element_blank(),
panel.margin = unit(2, "lines"))
return(plot)
})
output$oscars_timeline <- renderPlot({
# get data from dataframe
metric <- input$oscars_metric
df <- resource.oscars() %>%
arrange(YEAR) %>% # order by year
mutate(MOVIE = sprintf("%s (%s)", MOVIE, YEAR),
MOVIE = factor(MOVIE, levels=unique(MOVIE))) # convert to factor for fixing plot sort ordering
# plotting
mean_value = round(mean(df[, metric]), 2)
plot <- ggplot(df, aes_string(x="MOVIE", y=metric, fill="STUDIO")) +
geom_bar(stat="identity", alpha=0.5) +
geom_text(aes_string(label=metric),
size=3.5, hjust=0, fontface="italic") +
geom_hline(yintercept=mean_value, linetype="longdash", color="steelblue") +
scale_fill_manual(values=D3COLORMAP20) +
scale_y_continuous(breaks=pretty_breaks(5)) +
labs(title=sprintf("Annual Oscar Winners\n(by %s, mean: %s)", metric, mean_value),
x="Oscar Winners",
y=metric) +
theme(panel.background = element_blank(),
axis.ticks.y = element_blank())
plot <- plot + coord_flip() # flip plot coordinates
return(plot)
})
output$oscars_correlations <- renderPlot({
# get data from dataframe
df <- resource.oscars() %>% # select numeric columns for generating correlation matrix using reshape2's melt function
select(BOXOFFICE, NOMINATIONS, WINS, WIN_PERCENTAGE)
# plotting
plot <- ggplot(melt(cor(df)), aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
geom_text(aes(label=formatC(value, digits=2, format="f")), size=4) +
scale_fill_gradient(low="white", high="steelblue") +
labs(title="Oscars Correlation Matrix", x="", y="")
return(plot)
})
output$actors_boxoffice <- renderPlot({
# get data from dataframe
metric <- input$actors_movies_metric
df <- resource.actors() %>%
arrange_(metric) %>% # dplyr arrange_ is arrange but allowing passing strings, in our case we pass a dynamic reactive variable
mutate(PERSON = factor(PERSON, levels=unique(PERSON))) # convert to factor for fixing plot sort ordering
# Build colormap
actors <- unique(df$PERSON) # get list of unique actors in filtered dataframe
colormap <- helper.colormapper(actors, c(input$actors_actor1, input$actors_actor2, input$actors_actor3))
# plotting
mean_value = round(mean(df[, metric]), 2)
plot <- ggplot(df, aes_string(x="PERSON", y=metric, fill="PERSON")) +
geom_bar(stat="identity", alpha=0.75) +
geom_text(aes_string(label=metric),
size=3.5, hjust=0, fontface="italic") +
geom_hline(yintercept=mean_value, linetype="longdash", color="steelblue") +
scale_fill_manual(values=colormap) +
scale_y_continuous(breaks=pretty_breaks(5)) +
labs(title=sprintf("Top 50 Actors\n(by %s, mean: %s)", metric, mean_value),
x="Actors",
y=sprintf("%s", metric)) +
theme(panel.background = element_blank(),
legend.position = "none",
axis.ticks.y = element_blank())
# conditional best movie name layer
if(metric == "BEST_BO") {
plot <- plot + geom_text(aes(y=0, label=BEST_PICTURE), size=3.5, color="gray40", hjust=0, fontface="italic")
}
plot <- plot + coord_flip() # flip plot coordinates
return(plot)
})
output$directors_boxoffice <- renderPlot({
# get data from dataframe
metric <- input$directors_movies_metric
df <- resource.directors() %>%
arrange_(metric) %>% # dplyr arrange_ is arrange but allowing passing strings, in our case we pass a dynamic reactive variable
mutate(PERSON = factor(PERSON, levels=unique(PERSON))) # convert to fdirector for fixing plot sort ordering
# Build colormap
directors <- unique(df$PERSON) # get list of unique directors in filtered dataframe
colormap <- helper.colormapper(directors, c(input$directors_director1, input$directors_director2, input$directors_director3))
# plotting
mean_value = round(mean(df[, metric]), 2)
plot <- ggplot(df, aes_string(x="PERSON", y=metric, fill="PERSON")) +
geom_bar(stat="identity", alpha=0.75) +
geom_text(aes_string(label=metric),
size=3.5, hjust=0, fontface="italic") +
geom_hline(yintercept=mean_value, linetype="longdash", color="steelblue") +
scale_fill_manual(values=colormap) +
scale_y_continuous(breaks=pretty_breaks(5)) +
labs(title=sprintf("Top 50 Directors\n(by %s, mean: %s)", metric, mean_value),
x="Directors",
y=sprintf("%s", metric)) +
theme(panel.background = element_blank(),
legend.position = "none",
axis.ticks.y = element_blank())
# conditional best movie name layer
if(metric == "BEST_BO") {
plot <- plot + geom_text(aes(y=0, label=BEST_PICTURE), size=3.5, color="gray40", hjust=0, fontface="italic")
}
plot <- plot + coord_flip() # flip plot coordinates
return(plot)
})
output$producers_boxoffice <- renderPlot({
# get data from dataframe
metric <- input$producers_movies_metric
df <- resource.producers() %>%
arrange_(metric) %>% # dplyr arrange_ is arrange but allowing passing strings, in our case we pass a dynamic reactive variable
mutate(PERSON = factor(PERSON, levels=unique(PERSON))) # convert to fproducer for fixing plot sort ordering
# Build colormap
producers <- unique(df$PERSON) # get list of unique producers in filtered dataframe
colormap <- helper.colormapper(producers, c(input$producers_producer1, input$producers_producer2, input$producers_producer3))
# plotting
mean_value = round(mean(df[, metric]), 2)
plot <- ggplot(df, aes_string(x="PERSON", y=metric, fill="PERSON")) +
geom_bar(stat="identity", alpha=0.75) +
geom_text(aes_string(label=metric),
size=3.5, hjust=0, fontface="italic") +
geom_hline(yintercept=mean_value, linetype="longdash", color="steelblue") +
scale_fill_manual(values=colormap) +
scale_y_continuous(breaks=pretty_breaks(5)) +
labs(title=sprintf("Top 50 Producers\n(by %s, mean: %s)", metric, mean_value),
x="Producers",
y=sprintf("%s", metric)) +
theme(panel.background = element_blank(),
legend.position = "none",
axis.ticks.y = element_blank())
# conditional best movie name layer
if(metric == "BEST_BO") {
plot <- plot + geom_text(aes(y=0, label=BEST_PICTURE), size=3.5, color="gray40", hjust=0, fontface="italic")
}
plot <- plot + coord_flip() # flip plot coordinates
return(plot)
})
# =========================================================================
# Server outputs : Downloads
# =========================================================================
output$studios_download <- downloadHandler(
filename <- function() {
sprintf("studios_%s.csv", Sys.Date())
},
content <- function(filename) {
df <- resource.studios()
write.csv(df, file=filename, row.names=FALSE)
}
)
output$oscars_download <- downloadHandler(
filename <- function() {
sprintf("oscars_%s.csv", Sys.Date())
},
content <- function(filename) {
df <- resource.oscars()
write.csv(df, file=filename, row.names=FALSE)
}
)
output$actors_download <- downloadHandler(
filename <- function() {
sprintf("actors_%s.csv", Sys.Date())
},
content <- function(filename) {
df <- resource.actors()
write.csv(df, file=filename, row.names=FALSE)
}
)
output$directors_download <- downloadHandler(
filename <- function() {
sprintf("directors_%s.csv", Sys.Date())
},
content <- function(filename) {
df <- resource.directors()
write.csv(df, file=filename, row.names=FALSE)
}
)
output$producers_download <- downloadHandler(
filename <- function() {
sprintf("producers_%s.csv", Sys.Date())
},
content <- function(filename) {
df <- resource.producers()
write.csv(df, file=filename, row.names=FALSE)
}
)
})