-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.R
203 lines (190 loc) · 6.98 KB
/
test.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
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
library(dashBootstrapComponents)
library(ggplot2)
library(plotly)
library(reshape2)
library(tidyverse)
#Create App
app <- Dash$new(external_stylesheets = dbcThemes$BOOTSTRAP)
#Read in data/wrangle
games <- readr::read_csv(here::here('data', 'vgsales.csv'))
game_melt <- melt(data=games,id.vars = c("Rank","Name","Platform","Year","Genre","Publisher"),measure.vars=c("NA_Sales","EU_Sales","JP_Sales","Other_Sales","Global_Sales"))
game_melt$Year <- as.integer(game_melt$Year)
colnames(game_melt)[7] <- "Region"
colnames(game_melt)[8] <- "Copies Sold"
#Nested Lists for Filters
platform_filter <- unique(games$Platform) %>%
purrr::map(function(col) list(label = col, value = col))
platform_filter <- append(platform_filter,list(list(label="All",value="all")))
genre_filter <- unique(games$Genre) %>%
purrr::map(function(col) list(label = col, value = col))
genre_filter <- append(genre_filter,list(list(label="All",value="all")))
publisher_filter <- unique(games$Publisher) %>%
purrr::map(function(col) list(label = col, value = col))
publisher_filter <- append(publisher_filter,list(list(label="All",value="all")))
app$layout(
dbcContainer(
list(
htmlH1('Dashr heroky deployment'),
htmlLabel("Plot 1: Copies Sold vs Time"),
dccGraph(id='plot-area'),
htmlBr(),
htmlLabel("Plot 2: Number of Games Released vs Time"),
dccGraph(id='plot-area2'),
htmlBr(),
htmlLabel("Select your region of interest:"),
dccDropdown(
id='region_selector',
options = list(list(label="North America",value="NA_Sales"),
list(label="Europe",value="EU_Sales"),
list(label="Japan",value="JP_Sales"),
list(label="Other",value="Other_Sales"),
list(label="Global", value = "Global_Sales")),
value='Global_Sales',
multi=TRUE),
htmlBr(),
htmlLabel("Select your Platform of interest:"),
dccDropdown(
id='platform_selector',
options = platform_filter,
value="all",
multi=TRUE),
htmlBr(),
htmlLabel("Select your Genre of interest:"),
dccDropdown(
id='genre_selector',
options = genre_filter,
value="all",
multi=TRUE),
htmlBr(),
htmlLabel("Select your Publisher of interest:"),
dccDropdown(
id='publisher_selector',
options = publisher_filter,
value="all",
multi=TRUE),
htmlBr(),
htmlLabel('Slider'),
dccRangeSlider(
id = "year_selector",
min = 1980,
max = 2017,
marks = list("1980" = "1980",
"1985" = "1985",
"1990" = "1990",
"1995" = "1995",
"2000" = "2000",
"2005" = "2005",
"2010" = "2010",
"2015" = "2015"),
value = list(1980,2017))
)
)
)
app$callback(
output('plot-area', 'figure'),
list(input('region_selector', 'value'),
input('platform_selector', 'value'),
input('genre_selector', 'value'),
input('publisher_selector', 'value'),
input('year_selector', 'value')),
function(reg,plat,gen,pub,years) {
# Input: List of Regions, Platforms, Genres, Publishers, Min and Max Year
# Output: Graph
#
# Create subset based on filters
# Pass to graph
# Output graph
if ("Global_Sales" %in% reg){
filter_region = list("Global_Sales")
} else {
filter_region = reg
}
if ("all" %in% plat){
filter_plat = unique(game_melt$Platform)
} else {
filter_plat = plat
}
if ("all" %in% gen){
filter_gen = unique(game_melt$Genre)
} else {
filter_gen = gen
}
if ("all" %in% pub){
filter_pub = unique(game_melt$Publisher)
} else {
filter_pub = pub
}
min_year = years[1]
max_year = years[2]
graph1 <- game_melt[,3:8] %>%
subset(Region %in% filter_region & Platform %in% filter_plat & Genre %in% filter_gen & Publisher %in% filter_pub & Year >= min_year & Year <= max_year) %>%
group_by(Year,Genre) %>%
summarise("Copies Sold" = sum(`Copies Sold`)) %>%
ggplot() +
aes(x=as.factor(Year),
y=`Copies Sold`,
fill = Genre) +
geom_bar(stat="identity")+
theme(axis.text.x = element_text(angle = 90, hjust=0.95, vjust=0.2)) +
ylab("Number of Copies Sold (in millions)")+
xlab("Year")
return (ggplotly(graph1))
}
)
#Callback for Plot2
app$callback(
output('plot-area2', 'figure'),
list(input('region_selector', 'value'),
input('platform_selector', 'value'),
input('genre_selector', 'value'),
input('publisher_selector', 'value'),
input('year_selector', 'value')),
function(reg,plat,gen,pub,years) {
# Input: List of Regions, Platforms, Genres, Publishers, Min and Max Year
# Output: Graph
#
# Create subset based on filters
# Pass to graph
# Output graph
if ("Global_Sales" %in% reg){
filter_region = list("Global_Sales")
} else {
filter_region = reg
}
if ("all" %in% plat){
filter_plat = unique(game_melt$Platform)
} else {
filter_plat = plat
}
if ("all" %in% gen){
filter_gen = unique(game_melt$Genre)
} else {
filter_gen = gen
}
if ("all" %in% pub){
filter_pub = unique(game_melt$Publisher)
} else {
filter_pub = pub
}
min_year = years[1]
max_year = years[2]
graph2 <- game_melt[,3:8] %>%
subset(Region %in% filter_region & Platform %in% filter_plat & Genre %in% filter_gen & Publisher %in% filter_pub & Year >= min_year & Year <= max_year) %>%
group_by(Year,Genre) %>%
count(Year,Genre) %>%
rename(`Number of Releases`="n") %>%
ggplot() +
aes(x=as.factor(Year),
y=`Number of Releases`,
fill = Genre) +
geom_bar(stat="identity") +
theme(axis.text.x = element_text(angle = 90, hjust=0.95, vjust=0.2))+
ylab("Number of Games Released")+
xlab("Year")
return (ggplotly(graph2))
}
)
app$run_server(debug=T)