0

Am trying sentiment analysis where my shiny web interface would display pie chart with positive and negative slices. But I also want to display the percentage of both the slices. Which gives me this error: cannot coerce type 'closure' to vector of type 'character'. Please help me solve this. Note: Its a reactive web interface. Here is my full code for the server.r part.

    shinyServer(function(input, output) {
    #Search tweets and Clean the tweets
    TweetFrame<-function(twtList)
    {
    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\\Rahul\\Desktop\\Converge\\Positive-words.txt', what='character', comment.char=';')
   neg.words = scan('C:\\Users\\Rahul\\Desktop\\Converge\\Negative-words.txt', what='character', comment.char=';')

   wordDatabase<-function()
   {
   pos.words<<-c(pos.words, 'Congrats', 'prizes', 'prize', 'thanks', 'thnx', 'Grt', 'gr8', 'plz', 'trending', 'recovering', 'brainstorm', 'leader', 'power', 'powerful', 'latest')
   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)
}

#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() )})


#Pie
slices <- reactive ({ slices <- c(sum(table_final()$Positive), sum(table_final()$Negative)) 
pct <- round(slices/sum(slices)*100)
})
labels <- c("Positive", "Negative")
labels <- paste(labels, pct()) # add percents to labels 
labels <- paste(labels,"%",sep="")

library(plotrix)
output$piechart <- renderPlot({
pie3D(slices(), labels = labels, col=rainbow(length(labels)),explode=0.00, main="Sentiment Analysis") })

})
  • 2
    Hi, it will be a lot easier to help you if you add a minimal reproducible example. I recently created some guidelines that could help you with that [here](https://stackoverflow.com/questions/48343080/how-to-create-a-good-shiny-reproducible-example). – Florian Jan 21 '18 at 07:51
  • 2
    Try to change `paste(labels, pct())` to `paste(labels, pct)` – DJack Jan 21 '18 at 10:57

0 Answers0