This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.R
280 lines (229 loc) · 9.37 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
# Made with 💖 by Ankit Pandey (https://www.github.com/ankit2web)
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
# Installing package if not already installed
EnsurePackage<-function(x)
{x <- as.character(x)
if (!require(x,character.only=TRUE))
{
install.packages(pkgs=x,repos="https://mran.microsoft.com/")
require(x,character.only=TRUE)
}
}
#Identifying packages required
PrepareTwitter<-function()
{
EnsurePackage("twitteR")
EnsurePackage("stringr")
EnsurePackage("ROAuth")
EnsurePackage("RCurl")
EnsurePackage("ggplot2")
EnsurePackage("reshape")
EnsurePackage("tm")
EnsurePackage("RJSONIO")
EnsurePackage("wordcloud")
EnsurePackage("gridExtra")
EnsurePackage("plyr")
EnsurePackage("e1071")
}
PrepareTwitter()
shinyServer(function(input, output, session) {
consumer_api_key <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Enter your consumer key
consumer_api_secret <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Enter your consumer secret
access_token <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Enter your access token
access_token_secret <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Enter your access secret
setup_twitter_oauth(consumer_api_key, consumer_api_secret, access_token, access_token_secret) # Sets up the OAuth credentials for a twitteR session
#Search tweets and create a data frame -Stanton (2013)
# Clean the tweets
TweetFrame<-function(twtList)
{
df<- do.call("rbind",lapply(twtList,as.data.frame))
#removal of emoticons
df$text <- sapply(df$text,function(row) iconv(row, "latin1", "ASCII", sub="")) #If you wish to print emoticons just comment this line
df$text = gsub("(f|ht)tp(s?)://(.*)[.][a-z]+", "", df$text)
return (df$text)
}
# Function to create a data frame from tweets
pos.words = scan('positive_words.txt', what='character', comment.char=';') #Provide your path to "positive_words.txt" file
neg.words = scan('negative_words.txt', what='character', comment.char=';') #Provide your path to "negative_words.txt" file
wordDatabase<-function()
{
pos.words<<-c(pos.words)
neg.words<<-c(neg.words)
}
score.sentiment <- function(sentences, pos.words, neg.words, .progress='none')
{
require(plyr)
require(stringr)
list=lapply(sentences, function(sentence, pos.words, neg.words)
{
sentence = gsub('[[:punct:]]',' ',sentence)
sentence = gsub('[[:cntrl:]]','',sentence)
sentence = gsub('\\d+','',sentence)
sentence = gsub('\n','',sentence)
sentence = tolower(sentence)
word.list = str_split(sentence, '\\s+')
words = unlist(word.list)
pos.matches = match(words, pos.words)
neg.matches = match(words, neg.words)
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
pp=sum(pos.matches)
nn = sum(neg.matches)
score = sum(pos.matches) - sum(neg.matches)
list1=c(score, pp, nn)
return (list1)
}, pos.words, neg.words)
score_new=lapply(list, `[[`, 1)
pp1=score=lapply(list, `[[`, 2)
nn1=score=lapply(list, `[[`, 3)
scores.df = data.frame(score=score_new, text=sentences)
positive.df = data.frame(Positive=pp1, text=sentences)
negative.df = data.frame(Negative=nn1, text=sentences)
list_df=list(scores.df, positive.df, negative.df)
return(list_df)
}
#TABLE DATA
library(reshape)
sentimentAnalyser<-function(result)
{
#Creating a copy of result data frame
test1=result[[1]]
test2=result[[2]]
test3=result[[3]]
#Creating three different data frames for Score, Positive and Negative
#Removing text column from data frame
test1$text=NULL
test2$text=NULL
test3$text=NULL
#Storing the first row(Containing the sentiment scores) in variable q
q1=test1[1,]
q2=test2[1,]
q3=test3[1,]
qq1=melt(q1, var='Score')
qq2=melt(q2, var='Positive')
qq3=melt(q3, var='Negative')
qq1['Score'] = NULL
qq2['Positive'] = NULL
qq3['Negative'] = NULL
#Creating data frame
table1 = data.frame(Text=result[[1]]$text, Score=qq1)
table2 = data.frame(Text=result[[2]]$text, Score=qq2)
table3 = data.frame(Text=result[[3]]$text, Score=qq3)
#Merging three data frames into one
table_final=data.frame(Text=table1$Text, Positive=table2$value, Negative=table3$value, Score=table1$value)
return(table_final)
}
percentage<-function(table_final)
{
#Positive Percentage
#Renaming
posSc=table_final$Positive
negSc=table_final$Negative
#Adding column
table_final$PosPercent = posSc/ (posSc+negSc)
#Replacing Nan with zero
pp = table_final$PosPercent
pp[is.nan(pp)] <- 0
table_final$PosPercent = pp*100
#Negative Percentage
#Adding column
table_final$NegPercent = negSc/ (posSc+negSc)
#Replacing Nan with zero
nn = table_final$NegPercent
nn[is.nan(nn)] <- 0
table_final$NegPercent = nn*100
return(table_final)
}
wordDatabase()
twtList<-reactive({twtList<-searchTwitter(input$searchTerm, n=input$maxTweets, lang="en") })
tweets<-reactive({tweets<-TweetFrame(twtList() )})
result<-reactive({result<-score.sentiment(tweets(), pos.words, neg.words, .progress='none')})
table_final<-reactive({table_final<-sentimentAnalyser( result() )})
table_final_percentage<-reactive({table_final_percentage<-percentage( table_final() )})
output$tabledata<-renderTable(table_final_percentage())
#WORDCLOUD
wordclouds<-function(text)
{
library(tm)
library(wordcloud)
corpus <- VCorpus(VectorSource(text)) #Fixed Corpus Transformation issue
#clean text
clean_text <- tm_map(corpus, removePunctuation)
#clean_text <- tm_map(clean_text, content_transformation)
clean_text <- tm_map(clean_text, content_transformer(tolower))
clean_text <- tm_map(clean_text, removeWords, stopwords("english"))
clean_text <- tm_map(clean_text, removeNumbers)
clean_text <- tm_map(clean_text, stripWhitespace)
return (clean_text)
}
text_word<-reactive({text_word<-wordclouds( tweets() )})
output$word <- renderPlot({ wordcloud(text_word(),random.order=F,max.words=80, col=rainbow(100), main="WordCloud", scale=c(4.5, 1)) })
#HISTOGRAM
output$histPos<- renderPlot({ hist(table_final()$Positive, col=rainbow(10), main="Histogram of Positive Sentiment", xlab = "Positive Score") })
output$histNeg<- renderPlot({ hist(table_final()$Negative, col=rainbow(10), main="Histogram of Negative Sentiment", xlab = "Negative Score") })
output$histScore<- renderPlot({ hist(table_final()$Score, col=rainbow(10), main="Histogram of Score Sentiment", xlab = "Overall Score") })
#Pie
slices <- reactive ({ slices <- c(sum(table_final()$Positive), sum(table_final()$Negative)) })
labels <- c("Positive", "Negative")
library(plotrix)
output$piechart <- renderPlot({ pie3D(slices(), labels = labels, col=rainbow(length(labels)),explode=0.00, main="Sentiment Analysis") })
#TOP TRENDING TOPICS
toptrends <- function(place)
{
a_trends = availableTrendLocations()
woeid = a_trends[which(a_trends$name==place),3]
trend = getTrends(woeid)
trends = trend[1:2]
dat <- cbind(trends$name)
dat2 <- unlist(strsplit(dat, split=", "))
dat3 <- grep("dat2", iconv(dat2, "latin1", "ASCII", sub="dat2"))
dat4 <- dat2[-dat3]
return (dat4)
}
trend_table<-reactive({ trend_table<-toptrends(input$trendingTable) })
output$trendtable <- renderTable(trend_table())
#TOP CHARTS
# Top charts of a particular hashtag - Barplot
toptweeters<-function(tweetlist)
{
tweets <- twListToDF(tweetlist)
tweets <- unique(tweets)
# Make a table for the number of tweets per user
d <- as.data.frame(table(tweets$screenName))
d <- d[order(d$Freq, decreasing=T), ] #descending order of top charts according to frequency of tweets
names(d) <- c("User","Tweets")
return (d)
}
# Plot the table above for the top 20 charts
d<-reactive({d<-toptweeters( twtList() ) })
output$tweetersplot<-renderPlot ( barplot(head(d()$Tweets, 20), names=head(d()$User, 20), horiz=F, las=2, main="Top 20 users associated with the Hashtag", col=1) )
output$tweeterstable<-renderTable(head(d(),20))
#TOP HASHTAGS OF USER
tw1 <- reactive({ tw1 = userTimeline(input$user, n = 3200) })
tw <- reactive({ tw = twListToDF(tw1()) })
vec1<-reactive ({ vec1 = tw()$text })
extract.hashes = function(vec){
hash.pattern = "#[[:alpha:]]+"
have.hash = grep(x = vec, pattern = hash.pattern)
hash.matches = gregexpr(pattern = hash.pattern,
text = vec[have.hash])
extracted.hash = regmatches(x = vec[have.hash], m = hash.matches)
df = data.frame(table(tolower(unlist(extracted.hash))))
colnames(df) = c("tag","freq")
df = df[order(df$freq,decreasing = TRUE),]
return(df)
}
dat<-reactive({ dat = head(extract.hashes(vec1()),50) })
dat2<- reactive ({ dat2 = transform(dat(),tag = reorder(tag,freq)) })
p<- reactive ({ p = ggplot(dat2(), aes(x = tag, y = freq)) + geom_bar(stat="identity", fill = "blue")
p + coord_flip() + labs(title = "Hashtag frequencies in the tweets of the Twitter User") })
output$tophashtagsplot <- renderPlot ({ p() })
}) #shiny server