-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
187 lines (168 loc) · 5.61 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
source("dependencies.r")
server <- function(input, output) {
# reactive expression to handle hashtag input in SYUZNET approach
getHashtag <- reactive({
if (input$hashtag == "") {
shinyalert(title = "Oops!",
text = "Please enter a hashtag",
type = "error")
}
validate(need(input$hashtag != "", message = ""))
input$hashtag
})
# reactive expression to handle hashtag input in ML approach
getHashtagML <- reactive({
if (input$hashtagML == "") {
shinyalert(title = "Oops!",
text = "Please enter a hashtag",
type = "error")
}
validate(need(input$hashtagML != "", message = ""))
input$hashtagML
})
# reactive expression to get tweets
tweets <- reactive({
if (input$sampleSize < 100 || input$sampleSize > 1000) {
shinyalert(title = "Oops!",
text = "Please enter a valid sample size",
type = "error")
}
validate(need(input$sampleSize >= 100 &&
input$sampleSize <= 1000, message = ""))
getTweets(getHashtag(), input$sampleSize)
})
# reactive expression for ML sample size input
tweetsML <- reactive({
if (input$sampleSizeML < 100 || input$sampleSizeML > 1000) {
shinyalert(title = "Oops!",
text = "Please enter a valid sample size",
type = "error")
}
validate(need(
input$sampleSizeML >= 100 &&
input$sampleSizeML <= 1000,
message = ""
))
getTweets(getHashtagML(), input$sampleSizeML)
})
# reactive expression to calculate the word tweet frequency
tweetsFreqDf <- reactive({
df <- getTweetsFreqDf(tweets())
data.frame(Word = df$word, Frequency = df$freq)
})
# reactive expression to calculate the sentiments
sentimentDf <- reactive({
tweetsText <- tweets()$cleanText
get_nrc_sentiment(tweetsText)
})
# get knn confusion matrix
getKNNCF <- reactive({
p <- shiny::Progress$new()
p$set(message = "Training and testing model to compute confusion matrix")
cm <- getKNNConfusionMatrix(useCache = input$useCache)
on.exit(p$close())
return(cm)
})
# reactive expression for getting SVN confusion matrix
getSVNCF <- reactive({
p <- shiny::Progress$new()
p$set(message = "Training and testing model to compute confusion matrix")
cm <- getSVMConfusionMatrix(useCache = input$useCache)
on.exit(p$close())
return(cm)
})
# callback for frequencyTable TAB
output$frequencyTable <- renderDataTable(tweetsFreqDf())
# callback for wordcloud TAB
output$wordcloud <- renderWordcloud2({
wordcloud2(tweetsFreqDf()[1:100, ])
})
# callback for tweets TAB
output$tweets <- renderDataTable({
df <- tweets()
compact <-
data.frame(
date = df$created_at,
userid = df$screen_name,
tweet = df$text
)
compact
})
# output ploit for sentiment analysis
output$sentimentAnalysisPlot <- renderPlot({
df <- sentimentDf()
barplot(
colSums(df),
las = 2,
col = rainbow(10),
ylab = "Count",
main = paste(
"Sentiment Scores for",
paste("#", getHashtag(), sep = ""),
"Tweets"
)
)
})
# output plot for KNN confusion matrix
output$knnCMPlot <- renderPlot({
cm <- getKNNCF()
fourfoldplot(cm[["table"]],
main = "KNN Confusion matrix",
conf.level = 0,
margin = 1)
})
# Output SVM plot
output$svmPlot <- renderPlot({
p <- shiny::Progress$new()
p$set(message = "Training model and plotting graph")
on.exit(p$close())
plot(getSVMTrainedSentimentAnalysisModel(useCache = input$useCache))
})
# Output KNN plot
output$knnPlot <- renderPlot({
p <- shiny::Progress$new()
p$set(message = "Training model and plotting graph")
on.exit(p$close())
plot(getKNNTrainedSentimentAnalysisModel(useCache = input$useCache))
})
# output plot for SVN confusion matrix
output$svmCMPlot <- renderPlot({
cm <- getSVNCF()
fourfoldplot(cm[["table"]],
main = "SVM Confusion matrix",
conf.level = 0,
margin = 1)
})
# sentiment analysis plot using SVM
output$mlSentimentPlotSVM <- renderPlot({
p <- shiny::Progress$new()
p$set(message = "Gathering Tweets and analyzing sentiment")
on.exit(p$close())
tweetsDf <- tweetsML()
tweetsDf$s <- svmPredict(tweetsDf$text)
ggplot(data = tweetsDf) + labs(title = "SVM Sentiment analysis",
xlabel = "Sentiment",
ylabel = "Count") + geom_histogram(
mapping = aes(x = s,),
fill = "#42a5f5",
color = "white",
stat = "count"
)
})
# sentiment analysis plot using KNN
output$mlSentimentPlotKNN <- renderPlot({
p <- shiny::Progress$new()
p$set(message = "Gathering Tweets and analyzing sentiment")
on.exit(p$close())
tweetsDf <- tweetsML()
tweetsDf$s <- knnPredict(tweetsDf$text)
ggplot(data = tweetsDf) + labs(title = "KNN Sentiment analysis",
xlabel = "Sentiment",
ylabel = "Count") + geom_histogram(
mapping = aes(x = s),
fill = "#42a5f5",
color = "white",
stat = "count",
)
})
}