0

I get this error when I run my code:

Warning: Error in : `.x` is not a vector (closure)
Stack trace (innermost first):
    41: map
    40: map_df
    39: as_data_frame
    38: tbl_df
    37: server [C:\Users\Jasmeet\Documents/app.R#106]
     1: runApp
Error : `.x` is not a vector (closure)

I am not able to extract tweets based on data entered by a user in a textbox. There is probably an error in the functioning of the reactive objects where a user selects the number of tweets to be considered and the twitter account of which it wants the analysis to be done.

  library(shiny)

ui <- fluidPage(

  headerPanel("Twitter Sentiment Analysis"),

  sidebarPanel(

    textInput("searchTerm", "Enter data to be searched with '@'", "@"),
    sliderInput("maxTweets","Number of recent tweets to use for analysis:",min=5,max=1000,value=500), 
    submitButton(text="Analyse")
  ),

  mainPanel(
    tabsetPanel(
      tabPanel("Histogram",HTML
               ("<div><h3> Histograms graphically depict the positivity or negativity of peoples' opinion about of the hashtag
                 </h3></div>"),plotOutput("histScore")
               )

      )

  )

)


server <- function(input, output) {

  library(twitteR)
  library(purrr)
  library(dplyr)
  require('ROAuth')
  require('RCurl')
  library(plyr)
  library(stringr)
  library(reprex)

  #word database
  pos.words = scan('C:/Users/Jasmeet/Desktop/R/poswords/positive-words.txt',what = 'character',comment.char = ';')
  neg.words = scan('C:/Users/Jasmeet/Desktop/R/negwords/negative-words.txt',what = 'character',comment.char = ';')     


  ##NEW
  score.sentiment <- function(sentences, pos.words, neg.words, .progress='none')
  {
    require(plyr)
    require(stringr)
    scores <- laply(sentences, function(sentence, pos.words, neg.words){
      sentence <- gsub('[[:punct:]]', "", sentence)
      sentence <- gsub('[[:cntrl:]]', "", sentence)
      sentence <- gsub('\\d+', "", sentence)
      sentence <- tolower(sentence)
      word.list <- strsplit(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)
      score <- sum(pos.matches) - sum(neg.matches)
      return(score)
    }, pos.words, neg.words, .progress = .progress)
    scores.df <- data.frame(score=scores, text=sentences)
    return(scores.df)
  }


  consumerKey <- "xxx"
  reqURL <- "xxx"
  accessURL <- "xxx"
  authURL <-"xxx"
  consumerSecret <-"xxx"
  accessToken <- "xxx"
  accessTokenSecret <- "xxx"

  setup_twitter_oauth(consumerKey,consumerSecret,accessToken,accessTokenSecret)

enter code here

  #Extracting Tweets
  tweet1 <- reactive({ tweet1 = userTimeline(input$searchTerm, n=input$maxTweets) })

  #Converting to dataframe


  tweet_df <- tbl_df(map_df(tweet1, as.data.frame))

  bscore <- score.sentiment(tweet_df$text,pos.words,neg.words,.progress='text')


  ###histogram 

  output$histScore<- renderPlot({ hist(bscore$score, col=rainbow(10), main="Histogram of Positive Sentiment") }) 


}
shinyApp(ui = ui, server = server)
Graham
  • 7,431
  • 18
  • 59
  • 84
  • Can you make a reproducible example? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Maybe something with dput(), remove all the authentication stuff and just hard code the data – Chris Beeley Feb 08 '18 at 17:05
  • Code runs fine when I replace - #Extracting Tweets `tweet1 <- reactive({ tweet1 = userTimeline(input$searchTerm, n=input$maxTweets) })` with `tweet1 <- userTimeline("@FCBarcelona",n=100) ` but now the app does not accept's the data enterd by user. – Jasmeet Singh Feb 08 '18 at 17:24

0 Answers0