-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.r
385 lines (260 loc) · 12.8 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#SERVER.R
# Server is a function used to render the objects created in the User Interface function of the shiny Application
# It takes the input and output as an argument
server = function(input, output)
{
# Creating reactive to the input actionButton 'goButton' that was created in the the ui function
# eventReactive - Responds to "event-like" reactive inputs, values, and expressions.
data1 = eventReactive(input$goButton, {
# Conditional data extraction based on user input, if the InputID selected by user = username
if (input$typeInput == "username")
{
#tweetOutput <- userTL(user.name = input$usernameInput,number.of.tweets = input$numberInput)
tweetOutput <- searchTwitter(input$companyInput, input$numberInput, lang="en", since="2014-08-20")
}
#Cleans the tweet
df.tweets = cleanTweets(tweetOutput)
#Get sentiments
nrc.lexicons = get_nrc_sentiment(df.tweets$text_clean)
})
# ## Rendering TM plots ##
# renderPlot - Renders a reactive plot that is suitable for assigning to an output slot.
# In this case the the objects used are plot1 and plot2
output$plot1 = renderPlot({
# ## creating Barplot for emotions ##
barplot(
#Sort - (or order) a vector or factor (partially) into ascending or descending order
#prop.table - Express Table Entries as Fraction of Marginal Table
sort(colSums(prop.table(data1()[, 1:8]))),
horiz = TRUE,
cex.names = 0.8,
las = 1,
main = "Emotions in tweets", xlab="Percentage", xlim = c(0,.4))},
width = 700, height = 500)
output$plot2 = renderPlot({
# ## Creating barplot for positive vs negative ##
barplot(
sort(colSums(prop.table(data1()[, 9:10]))),
horiz = TRUE,
cex.names = 0.75,
las = 1,
main = "Ratio of positive to negative tweets",xlab="Percentage", xlim = c(0,1))},
width = 700, height = 500)
# Creating reactive to the input actionButton 'goButton' that was created in the the ui function
# eventReactive - Responds to "event-like" reactive inputs, values, and expressions.
data2 = eventReactive(input$goButton, {
if (input$typeInput == "username")
{
searchQuery = input$companyInput;
if(grepl(',',input$problemInput,fixed=TRUE) == "TRUE") {
problemList <- strsplit(input$problemInput,",")[[1]]
for (problem in problemList) {
searchQuery <- paste(searchQuery, problem, sep = " AND ")
}
} else {
searchQuery <- paste(searchQuery, input$problemInput, sep = " AND ")
}
if(input$latInput == "") {
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en")
} else {
geoString = input$latInput
geoString <- paste(geoString, input$longInput, sep = ",")
geoString <- paste(geoString, input$rediuslemInput, sep = ",")
geoString <- paste(geoString, "mi")
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en", geocode = geoString)
}
}
searchtweet.clean = cleanTweets(tweetOutput)
searchtweet.tdm.tfidf = tdm.TFIDF(searchtweet.clean)
nrc.lex = getSentiments.TF_IDF.nrc(searchtweet.tdm.tfidf)
})
# ## Creating a Render plots for TFIDF ##
# renderPlot - Renders a reactive plot that is suitable for assigning to an output slot.
# In this case the the objects used are plot3 and plot4
output$plot3 = renderPlot({
barplot(
sort(colSums(prop.table(data2()[, 1:8]))),
horiz = TRUE,
cex.names = 0.75,
las = 1,
main = "Emotions in tweets", xlab="Percentage",xlim = c(0,.4))}, width = 700, height = 500)
output$plot4 = renderPlot({
barplot(
sort(colSums(prop.table(data2()[, 9:10]))),
horiz = TRUE,
cex.names = 0.8,
las = 1,
main = "Polarity in tweets", xlab="Percentage", xlim = c(0,1))}, width = 700, height = 500)
# Creating reactive to the input actionButton 'goButton' that was created in the the ui function
# eventReactive - Responds to "event-like" reactive inputs, values, and expressions.
data3 = eventReactive(input$goButton, {
if (input$typeInput == "username")
{
searchQuery = input$companyInput;
if(grepl(',',input$problemInput,fixed=TRUE) == "TRUE") {
problemList <- strsplit(input$problemInput,",")[[1]]
for (problem in problemList) {
searchQuery <- paste(searchQuery, problem, sep = " AND ")
}
} else {
searchQuery <- paste(searchQuery, input$problemInput, sep = " AND ")
}
if(input$latInput == "") {
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en")
} else {
geoString = input$latInput
geoString <- paste(geoString, input$longInput, sep = ",")
geoString <- paste(geoString, input$rediuslemInput, sep = ",")
geoString <- paste(geoString, "mi")
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en", geocode = geoString)
}
}
df.tweets <- cleanTweets(tweetOutput)
searchtweet.tdm.tm.stopword = tdm.tmStopWord(df.tweets)
tweets.positive = generateWordCloud.positive.tmStopWords(searchtweet.tdm.tm.stopword)
})
# ## Render Positive Wordcloud TM ##
# renderWordcloud2 - Renders a reactive word cloud that is suitable for assigning to an output slot.
# In this case the the object used is wordCloud1
output$wordCloud1 = renderWordcloud2({wordcloud2(data = data3())})
# Creating reactive to the input actionButton 'goButton' that was created in the the ui function
# eventReactive - Responds to "event-like" reactive inputs, values, and expressions.
data4 = eventReactive(input$goButton, {
if (input$typeInput == "username")
{
searchQuery = input$companyInput;
if(grepl(',',input$problemInput,fixed=TRUE) == "TRUE") {
problemList <- strsplit(input$problemInput,",")[[1]]
for (problem in problemList) {
searchQuery <- paste(searchQuery, problem, sep = " AND ")
}
} else {
searchQuery <- paste(searchQuery, input$problemInput, sep = " AND ")
}
if(input$latInput == "") {
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en")
} else {
geoString = input$latInput
geoString <- paste(geoString, input$longInput, sep = ",")
geoString <- paste(geoString, input$rediuslemInput, sep = ",")
geoString <- paste(geoString, "mi")
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en", geocode = geoString)
}
}
df.tweets = cleanTweets(tweetOutput)
searchtweet.tdm.tm.stopword = tdm.tmStopWord(df.tweets)
tweets.negative = generateWordCloud.negative.tmStopWords(searchtweet.tdm.tm.stopword)
})
# ## Render negative wordcloud TM ##
# renderWordcloud2 - Renders a reactive word cloud that is suitable for assigning to an output slot.
# In this case the the object used is wordCloud2
output$wordCloud2 = renderWordcloud2({wordcloud2(data = data4())})
# Creating reactive to the input actionButton 'goButton' that was created in the the ui function
# eventReactive - Responds to "event-like" reactive inputs, values, and expressions.
data5 = eventReactive(input$goButton, {
if (input$typeInput == "username")
{
searchQuery = input$companyInput;
if(grepl(',',input$problemInput,fixed=TRUE) == "TRUE") {
problemList <- strsplit(input$problemInput,",")[[1]]
for (problem in problemList) {
searchQuery <- paste(searchQuery, problem, sep = " AND ")
}
} else {
searchQuery <- paste(searchQuery, input$problemInput, sep = " AND ")
}
if(input$latInput == "") {
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en")
} else {
geoString = input$latInput
geoString <- paste(geoString, input$longInput, sep = ",")
geoString <- paste(geoString, input$rediuslemInput, sep = ",")
geoString <- paste(geoString, "mi")
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en", geocode = geoString)
}
}
df.tweets = cleanTweets(tweetOutput)
tdm.tfidf = tdm.TFIDF(df.tweets)
tdm.tm.nostop = tdm.tm(df.tweets)
tweets.positive = generateWordCloud.positive.TF_IDF(tdm.tfidf, tdm.tm.nostop)
})
# ##Rendering positive wordcloud for TFIDF ## #
# renderWordcloud2 - Renders a reactive word cloud that is suitable for assigning to an output slot.
# In this case the the object used is wordCloud3
output$wordCloud3 = renderWordcloud2({wordcloud2(data = data5())})
# Creating reactive to the input actionButton 'goButton' that was created in the the ui function
# eventReactive - Responds to "event-like" reactive inputs, values, and expressions.
data6 = eventReactive(input$goButton, {
if (input$typeInput == "username")
{
searchQuery = input$companyInput;
if(grepl(',',input$problemInput,fixed=TRUE) == "TRUE") {
problemList <- strsplit(input$problemInput,",")[[1]]
for (problem in problemList) {
searchQuery <- paste(searchQuery, problem, sep = " AND ")
}
} else {
searchQuery <- paste(searchQuery, input$problemInput, sep = " AND ")
}
if(input$latInput == "") {
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en")
} else {
geoString = input$latInput
geoString <- paste(geoString, input$longInput, sep = ",")
geoString <- paste(geoString, input$rediuslemInput, sep = ",")
geoString <- paste(geoString, "mi")
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en", geocode = geoString)
}
}
df.tweets = cleanTweets(tweetOutput)
tdm.tfidf = tdm.TFIDF(df.tweets)
tdm.tm.nostop = tdm.tm(df.tweets)
tweets.negative = generateWordCloud.negative.TF_IDF(tdm.tfidf, tdm.tm.nostop)
tweets.negative
})
# ##Render negative wordcloud TFIDF## #
# renderWordcloud2 - Renders a reactive word cloud that is suitable for assigning to an output slot.
# In this case the the object used is wordCloud4
output$wordCloud4 = renderWordcloud2({wordcloud2(data = data6())})
# Creating reactive to the input actionButton 'goButton' that was created in the the ui function
# eventReactive - Responds to "event-like" reactive inputs, values, and expressions.
data7 = eventReactive(input$goButton, {
if (input$typeInput == "username")
{
searchQuery = input$companyInput;
if(grepl(',',input$problemInput,fixed=TRUE) == "TRUE") {
problemList <- strsplit(input$problemInput,",")[[1]]
for (problem in problemList) {
searchQuery <- paste(searchQuery, problem, sep = " AND ")
}
} else {
searchQuery <- paste(searchQuery, input$problemInput, sep = " AND ")
}
if(input$latInput == "") {
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en")
} else {
geoString = input$latInput
geoString <- paste(geoString, input$longInput, sep = ",")
geoString <- paste(geoString, input$rediuslemInput, sep = ",")
geoString <- paste(geoString, "mi")
tweetOutput <- searchTwitter(searchQuery, input$numberInput, lang="en", geocode = geoString)
}
}
#Converting the Tweets into data frame
#df.tweets = twListToDF(tweetOutput)
df.tweets = cleanTweets(tweetOutput)
#only displaying Text, Created, Screen Name, RT count, and Location
# Remove all nongraphical characters
text = str_replace_all(df.tweets$text,"[^[:graph:]]", " ")
df.tweets = cbind(text, df.tweets[c(5,11,3,12,17)])
#Changing column names
colnames(df.tweets) = c("Tweets", "Date", "Username", "Fav Count", "RT Count", "Location")
tweetOutput = df.tweets
#grep('/'+input$companyInput+'/', ignore.case = FALSE, rownames(tweetOutput));
#tweetOutput <- tweetOutput[grep('/'+input$companyInput+'/', rownames(tweetOutput)), ]
})
# ##Render tweets## #
# renderDataTable - Renders a reactive data table that is suitable for assigning to an output slot.
# In this case the the object used is tweetTable
output$tweetTable = renderDataTable({data7()}, options = list(lengthMenu = c(10, 30, 50), pageLength = 5))
}