avinax
avinax

Reputation: 127

Twitter analysis Shiny App

I want to create a basic shiny app wherein I can type a keyword in the text input box and when i click submit the output should be a Data table of the recent tweets having the keyword typed in the text input box. I also need to find a way to automatically enable the handshake between my app and twitter using setup_twitter_oauth. I have created the following app.R file

library(shiny)
library(twitteR)


ui <- fluidPage(
  titlePanel("Basic Twitter Search App"),
  textInput("twitter", "Search Keyword"),
  actionButton("click", label = "Search Tweets"),
  dataTableOutput("table")
)

server <- function(input, output){
  source(file = 'oauth.RData') #file containing the credentials
  output$table <- renderDataTable
    (
      {
    observeEvent(input$twitter, {searchTwitter(input$twitter, n=1500)
    })

  })
}

shinyApp(ui = ui, server = server)

but when I run the code (Run App), the following error occurs :

Error in orig(name = name, shinysession = self) : unused arguments (name = name, shinysession = self) Warning: Unhandled error in observer: client error: (400) Bad Request observeEvent(input$twitter)

Upvotes: 1

Views: 1323

Answers (2)

avinax
avinax

Reputation: 127

Thank You @Jimbo. After many failed experiments, the following code worked:

library(shiny)

ui <- fluidPage(
  textInput("handle", "Search Tweets:"),
  sliderInput("maxTweets","Number of recent tweets to use for analysis:",min=5,max=1500, value = 5),
  downloadButton("download", "Download File"),
  dataTableOutput("table")
)


server <- function(input, output) {


  library(twitteR)

  consumerKey = "My key"   
  consumerSecret = "My secret"
  accessToken = "My token"
  accessSecret = "My secret"
  my_oauth <- setup_twitter_oauth(consumer_key = consumerKey, consumer_secret = consumerSecret,
                                  access_token = accessToken, access_secret = accessSecret)

  output$table <- renderDataTable({
    TweetFrame<-function(searchTerm, maxTweets)
    {
      twtList<-searchTwitter(searchTerm,n=maxTweets)
      twtList1<- do.call("rbind",lapply(twtList,as.data.frame))
      twtList1$text<-iconv(twtList1$text, 'UTF-8', 'ASCII') #WILL THIS SOLVE THE UTF ENCODING PROBLEM: http://lists.hexdump.org/pipermail/twitter-users-hexdump.org/2013-May/000335.html
      return(twtList1)

    }
    entity1<-reactive({entity1<-TweetFrame(input$handle, input$maxTweets)})
    output$table <- renderDataTable({tab<-entity1()[1]})
    output$download <- downloadHandler(filename = function() {paste(input$handle, '.csv', sep='')},
                                       content = function(file){
                                         write.csv(entity1(), file)
                                       }
                                       )
  })
}

shinyApp(ui = ui, server = server)

Although I still haven't been able to figure out how to automatically enable the user authentication (without user intervention). Any help in this regards will be greatly appreciated.

Upvotes: 2

Jimbo
Jimbo

Reputation: 938

server <- function(input, output){
  source(file = 'oauth.RData') #file containing the credentials
  output$table <- renderDataTable({
    test <- searchTwitter(input$twitter, n=1500)
    return(test)
  })
}

This should work as long as searchTwitter returns a df or a matrix

Upvotes: 1

Related Questions