-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
78 lines (62 loc) · 2.4 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
library(shiny)
library(twitteR)
library(httr)
library(wordcloud)
library(wordcloud2)
library(tm)
library(shinyWidgets)
library(plotly)
library(ggplot2)
library(syuzhet)
library(lubridate)
library(scales)
library(reshape2)
library(dplyr)
api_key <- "660MFtiE7EV1yWm0oy9yirQom"
api_secret <- "mljqG0EvUaJzjKPhehvAQAVWZKOVQOs108Mz9vjNHdOvmnnSBb"
access_token <- "1710165894-IwLhzXcJh7kYCQ4OEWqafJeucEgPh17VtRsgz32"
access_token_secret <- "x8NsVG64rQioZNbPEZvFQgoLP1UaLJpl4wBqfJq5pJG3L"
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
shinyServer(function(input, output) {
tweet<-reactive({
t<-getUser(input$new)
tweets<-userTimeline(t,n=input$slider,includeRts = FALSE,excludeReplies = TRUE)
data <- twListToDF(tweets)
data$text <- sapply(data$text,function(row) iconv(row,"latin1","ASCII",sub=""))
corpus<-data$text
corpus<-Corpus(VectorSource(corpus))
corpus<-tm_map(corpus,tolower)
corpus<-tm_map(corpus,removeNumbers)
clean.corpus<-tm_map(corpus,removeWords,stopwords('en'))
corpus<-tm_map(corpus,removePunctuation)
remove.Url<-function(x) gsub('http[[:alnum:]]*','',x)
clean.corpus<-tm_map(clean.corpus,content_transformer(remove.Url))
clean.corpus<-tm_map(clean.corpus,stripWhitespace)
tdm<-TermDocumentMatrix(clean.corpus)
tdm<-as.matrix(tdm)
w<-rowSums(tdm)
tab<-data.frame(names(w),w)
colnames(tab)<-c('word','freq')
return(tab)
})
output$wordcloud<-renderWordcloud2({
wordcloud2(tweet(),size=0.7,shape='triangle',rotateRatio = 0.5,minSize=1)
})
output$barplot<-renderPlotly({
t<-getUser(input$new)
tweets<-userTimeline(t,n=input$slider,includeRts = FALSE,excludeReplies = TRUE)
data <- twListToDF(tweets)
data$text <- sapply(data$text,function(row) iconv(row,"latin1","ASCII", sub=""))
vector<-data$text
s <- get_nrc_sentiment(vector)
sentiment<-c("anger","anticipation","disgust","fear","joy","sadness","surprise","trust","negative","positive")
BD<-data.frame(sentiment,colSums(s))
colnames(BD)<-c("Sentiment","Score")
ggplot(data=BD, aes(x=Sentiment,y=Score)) +
geom_bar(stat="identity",fill=rainbow(10))+ylab("Scores")+xlab("Sentiment")+
labs(title="Sentiment Scores for Tweets extracted")+
theme_minimal()+
theme(axis.text.x=element_text(angle=20))
})
}
)