-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
301 lines (278 loc) · 11.1 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
### Server ###
server = function(input, output) {
# Function that makes it a bit less wordy to access annotations
annotationSubsets = reactive({
req(input$audioFeature)
filter(annotations, feature %in% input$audioFeature)
})
# Panel 2
output$featureDescription = renderText({
toString(annotationSubsets()[1,2])
})
output$featurePlot = renderPlot({
ggplot(taylor %>%
group_by(album_name) %>%
mutate(m = mean(eval(parse(text = input$audioFeature)))) %>%
arrange(m) %>%
ungroup() %>%
mutate(album_name = factor(album_name, unique(album_name)))) +
geom_joy(aes(x = eval(parse(text = input$audioFeature)),
y = fct_inorder(factor(album_name)),
fill = factor(album_name)),
scale = 1.5,
color = "black",
size = 1) +
theme_joy() +
labs(title = paste(str_to_title(input$audioFeature), "Distribution of Taylor Swift Albums"),
x = input$audioFeature,
y = "Album") +
scale_fill_manual(values = album_colors, breaks = albums) +
theme(legend.position = "none") +
xlim(c(0, 1)) +
theme(plot.title = element_text(size = 16))
})
output$featureTable = renderDataTable({
table = taylor %>%
group_by(album_name) %>%
summarise(!!paste("Mean", str_to_title(input$audioFeature)) := mean(eval(parse(text = input$audioFeature))))
table %>% arrange(desc(table[2])) %>%
rename(Album = 1) %>%
datatable(options = list(paging = F,
searching = F,
lengthChange = F)) %>%
formatRound(names(table)[2], digits = 4)
})
output$featureCommentary = renderText({
toString(annotationSubsets()[1,3])
})
# Panel 3
createWordCount = reactive({
req(input$cloudAlbum)
albumLyrics = allLyrics %>%
filter(album_name == input$cloudAlbum)
text = albumLyrics$lyric
words.vec = VectorSource(text)
words.corpus = Corpus(words.vec)
# Converting all text to lowercase
words.corpus = tm_map(words.corpus, content_transformer(tolower))
# Removing stop words from text
noStop = c("our", "ours", "yours")
stops = stopwords("english")[!stopwords("english") %in% noStop]
stops = c(stops, "oh", "ooh", "like", "know", "just", "gonna", "wanna", "cause", "yeah")
words.corpus = tm_map(words.corpus, removeWords, stops)
# Removing punctuation, but preserving single quotations
words.corpus = tm_map(words.corpus, removePunctuation, preserve_intra_word_contractions = T)
# Removing extra spaces
words.corpus = tm_map(words.corpus, stripWhitespace)
# Creating a term-document matrix (contains word frequencies)
dtm = TermDocumentMatrix(words.corpus)
m = as.matrix(dtm)
v = sort(rowSums(m), decreasing = TRUE)
data.frame(word = names(v), freq = v)
})
output$countTable = renderDataTable({
df = createWordCount()[1:450,]
rownames(df) = NULL
DT::datatable(df,
colnames = c("Word", "Frequency"),
caption = 'Note that stopwords such as i, me, my, am, etc. have been excluded. Hover over the wordcloud for specific word counts, or view the table below.')
})
cloudPalette = reactive({
if (input$cloudAlbum == "Taylor Swift") {
palette = palettes$taylorSwift
}
else if (input$cloudAlbum == "Fearless" | input$cloudAlbum == "Fearless (Taylor's Version)") {
palette = palettes$fearless
}
else if (input$cloudAlbum == "Speak Now") {
palette = palettes$speakNow
}
else if (input$cloudAlbum == "Red") {
palette = palettes$Red
}
else if (input$cloudAlbum == "Red (Taylor's Version)") {
palette = palettes$taylorRed
}
else if (input$cloudAlbum == "1989") {
palette = palettes$taylor1989
}
else if (input$cloudAlbum == "reputation") {
palette = palettes$reputation
}
else if (input$cloudAlbum == "Lover") {
palette = palettes$lover
}
else if (input$cloudAlbum == "folklore") {
palette = palettes$folklore
}
else if (input$cloudAlbum == "evermore") {
palette = palettes$evermore
}
else if (input$cloudAlbum == "Midnights") {
palette = palettes$midnights
}
})
output$wordCloud = renderWordcloud2({
wordcloud2(data = createWordCount()[1:450,],
fontFamily = "Helvetica",
fontWeight = "bold",
shape = 'circle',
ellipticity = 0.70,
color = rep_len(cloudPalette()[2:length(cloudPalette())],
length.out = nrow(createWordCount())),
backgroundColor = cloudPalette()[1],
size = 0.75)
})
# Panel 4
buttonPressed = eventReactive(input$button, {
if (input$numOfLines == 1) {
randNum = floor(runif(1, min = 1, max = nrow(allLyrics)))
randLyric = allLyrics$lyric[randNum]
randTrack = allLyrics$track_name[randNum]
randSection = allLyrics$element[randNum]
HTML(paste0(randLyric, "<br/><br/>", strong("from "), strong(em(randTrack)), em(strong(", ")), strong(randSection)))
}
else if (input$numOfLines == 2) {
randNum = floor(runif(1, min = 1, max = nrow(allLyrics)))
randLyric = allLyrics$lyric[randNum]
randTrack = allLyrics$track_name[randNum]
randSection = allLyrics$element[randNum]
if (allLyrics$element[randNum + 1] == randSection) {
start = randNum
end = randNum + 1
}
else {
start = randNum - 1
end = randNum
}
HTML(paste(allLyrics$lyric[start], "<br/>",
allLyrics$lyric[end],
"<br/><br/>", strong("from "), strong(em(randTrack)), em(strong(", ")), strong(randSection), sep = ""))
}
else if (input$numOfLines == 3) {
randNum = floor(runif(1, min = 1, max = nrow(allLyrics)))
randLyric = allLyrics$lyric[randNum]
randTrack = allLyrics$track_name[randNum]
randSection = allLyrics$element[randNum]
entireSection = randLyric
end = randNum
while (allLyrics$element[end + 1] == randSection) {
end = end + 1
}
start = randNum
while (allLyrics$element[start - 1] == randSection) {
start = start - 1
}
entireSection = allLyrics$lyric[start:end]
HTML(paste(paste(entireSection, collapse = "<br/>"),
"<br/><br/>", strong("from "), strong(em(randTrack)), em(strong(", ")), strong(randSection), sep = ""))
}
})
output$randGenerated = renderUI({
buttonPressed()
})
lexDivAlbum = function() {
lexicalDiv = allLyrics %>%
group_by(track_name, album_name) %>%
tidytext::unnest_tokens(word, lyric) %>%
summarise(LexicalDiversity = n_distinct(word) / length(word)) %>%
arrange(desc(LexicalDiversity))
}
output$lexDiversityAlbum = renderDataTable({
tabl = lexDivAlbum() %>%
group_by(album_name) %>%
summarise(`Mean Lexical Diversity` = mean(LexicalDiversity)) %>%
arrange(desc(`Mean Lexical Diversity`)) %>%
rename(Album = 1)
tabl %>%
datatable(options = list(paging = F,
searching = F,
lengthChange = F),
caption = "Taylor's albums arranged by average lexical diversity") %>%
formatRound(names(tabl)[2], digits = 4)
})
output$lexDiversitySong = renderDataTable({
tab = allLyrics %>%
tidytext::unnest_tokens(word, lyric) %>%
group_by(track_name) %>%
summarise(LexicalDiversity = n_distinct(word) / length(word)) %>%
arrange(desc(LexicalDiversity)) %>%
rename(Track = 1, `Lexical Diversity` = 2)
tab %>%
datatable(
caption = "This table displays the lexical diversity of each track in Taylor's 12 albums"
) %>%
formatRound(names(tab)[2], digits = 4)
})
output$lexDiversity = renderPlot({
lexicalDiv = lexDivAlbum()
lexicalDiv$album_name = gsub("(\\(Taylor's Version\\))", "TV", lexicalDiv$album_name)
pirateplot(formula = LexicalDiversity ~ album_name,
data = lexicalDiv,
theme = 0, #Starting the plot from nothing so it can be fully customised
pal = c("#B8BFE2", # 1989
"#727272",
"#731D05",
"#fdcdcd",
"#841E10",
"#00A3AD",
"#C3B377",
"#8449BB",
"#F6ED95",
"#526D85",
"#994914",
"#BABABA"), # folklore
main = "Lexical Diversity of Taylor Swift's Albums",
xlab = "Album",
ylab = "Lexical Diversity",
point.o = 0.6,
bean.f.o = 0,
bean.b.o = 0,
avg.line.o = 1,
point.pch = 21,
gl.col = "gray93",
sortx = "mean",
width.min = 1)
# Arrow and text for 1989
Arrows(x1 = 1.5, y1 = 0.40, x0 = 1.3, y0 = 0.3,
arr.type = "curved",
col = "#B8BFE2",
lwd = 3,
arr.length = 0.4)
text(x = 1.5, y = 0.445,
labels = paste("1989 has the least lexical diversity",
"\n", "of all Taylor's albums - consider",
"\n", "the repetitiveness of Shake It Off",
"\n", "and Welcome to New York"),
col = "#B8BFE2", cex = 0.92)
# Arrow and text for champagne problems
Arrows(x1 = 10.3, y1 = 0.585, x0 = 10.8, y0 = 0.577,
arr.type = "curved",
col = "#994914",
lwd = 3,
arr.length = 0.3)
text(x = 9, y = 0.592,
labels = paste("champagne problems is Taylor's",
"\n", "most lexically diverse song"),
col = "#994914", cex = 0.95)
# Arrow and text for Out of the Woods
Arrows(x1 = 1.5, y1 = 0.15, x0 = 1.13, y0 = 0.156,
arr.type = "curved",
col = "#B8BFE2",
lwd = 3,
arr.length = 0.3)
text(x = 2.9, y = 0.15,
labels = paste("\"Are we out of the woods yet?",
"\n", "Are we out of the woods yet?",
"\n", "Are we out of the woods yet?",
"\n", "Are we out of the woods?\""),
col = "#B8BFE2", cex = 0.95)
# Arrow and text for folklore
Arrows(x0 = 11.85, y0 = 0.33, x1 = 11.4, y1 = 0.23,
arr.type = "curved",
col = "#BABABA",
lwd = 3,
arr.length = 0.4)
text(x = 11, y = 0.2, labels = paste("Taylor's lyricism really", "\n", "shines through in folklore"), col = "#BABABA", cex = 0.95)
})
}