EnsurePackage<-function(x)
{x<-as.character(x)
if(!require(x,character.only = TRUE))
{
install.packages(pkgs=x,repos="http://cran.r-project.org")
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")
}
PrepareTwitter()
shinyServer(function(input,output){
#TABLE
#Search tweets and create a data frame
#Clean the tweets
TweetFrame<-function(twtList)
{
#for(i in 2:length(dates)){
#tweets <- c(tweets, searchTwitter(searchTerm, since=dates[i-1], until=dates[i], n=maxTweets))
#}
df<-do.call("rbind",lapply(twtList,as,data.frame))
#removes emoticons
df$text <- sapply(df$text,function(row) iconv(row, "latin1", "ASCII", sub=""))
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('C:/Users/User/Desktop/Sentiment UKM/pos.txt', what='character', comment.char=';')
neg.words=scan('C:/Users/User/Desktop/Sentiment UKM/neg.txt', what='character', comment.char=';')
wordDatabase<-function()
{
pos.words<<-c(pos.words, 'Congrats', 'prizes', 'prize', 'thanks', 'thnx', 'Grt', 'gr8', 'plz', 'trending', 'recovering', 'brainstorm', 'leader')
neg.words<<-c(neg.words, 'Fight', 'fighting', 'wtf', 'arrest', 'no', 'not')
}
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)
}
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()
#dates <- reactive({ as.Date(as.Date(Sys.Date()-input$noOfDays):as.Date(Sys.Date()), origin="1970-01-01") }) #SUGGESTION: Modify these dates of upto 7 days before running this program
#twtList<-reactive({twtList<-searchTwitter(input$searchTerm, n=input$maxTweets, since=dates()[1], until=dates()[2], lang="en") })
#for (i in 3:length(dates())) {
# twtList()<- c(twtList(), searchTwitter(input$searchTerm, n=input$maxTweets, since=dates()[i-1], until=dates()[i], lang="en"))
# }
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())
#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", xlab = "Overall Score")})
#PIE CHART
slices <- reactive({c(sum(table_final()$Positive), sum(table_final()$Negative)) })
labels <- c("Positive", "Negative")
library(plotrix)
#pie(slices(), labels = labels, col=rainbow(length(labels)), main="Sentiment Analysis")
output$piechart<-renderPlot({pie3D(slices(), labels = labels, col=rainbow(length(labels)),explode=0.00, main="Sentiment Analysis") })
}
)
Could you format this into a reproducible example? IE something folks can easily get up and running to replicate your issue? Currently, this is only part of a shiny app.
IF you aren't familiar with best practices for shiny reprexes, check out
This will make it easier for folks to repliate your issue and offer suggestions to solve it.
This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.