ropensci / rtweet

🐦 R client for interacting with Twitter's [stream and REST] APIs
https://docs.ropensci.org/rtweet
Other
785 stars 200 forks source link

Error while using browser based authentication from online shiny App #407

Closed deepakdeps closed 3 years ago

deepakdeps commented 4 years ago

Problem

I have been building a basic rshiny app to analyze twitter data. I currently don't have developer account so I have been getting the token from the individual account using browser based login.

This works fine while creating the shinyapp on my local laptop but when I am deploying the App on shinyapps.io and trying to search data using rtweet search functions, the shinyapp gets disconnected. On checking the log I got following message:

Requesting token on behalf of user... Warning: Error in : API user token required. see http://rtweet.info/articles/auth.html for instructions

Expected behavior

Reproduce the problem

## insert code here
server.R
shinyServer(function(input, output) {

     observeEvent(input$login, once = T, handlerExpr = {

        login_dummy <- search_tweets(
          "#COVID19", n = 1, include_rts = FALSE
        )
  })

  get_tweet_data<- function(a,b,c){
  print(a)
  print(b)
  print(c)
  temp=search_tweets(
          a, n = b, include_rts = c
        )

  return(temp) 
}

   get_word_cloud<- function(tweet_data){
  print("Print 1")
  print(tweet_data)
  print(dim(tweet_data))
  tweet_corpus = Corpus(VectorSource(tweet_data$text))
    print("Print 2")
  tweet_corpus = tm_map(tweet_corpus, content_transformer(tolower))
  tweet_corpus = tm_map(tweet_corpus, removeNumbers)
  tweet_corpus = tm_map(tweet_corpus, removePunctuation)
  tweet_corpus = tm_map(tweet_corpus, removeWords, c("the", "and","can","trash","one","lid","garbage","open", stopwords("english")))
  tweet_corpus =  tm_map(tweet_corpus, stripWhitespace)
  tweet_dtm_tfidf <- DocumentTermMatrix(tweet_corpus, control = list(weighting = weightTfIdf))
  freq = data.frame(sort(colSums(as.matrix(tweet_dtm_tfidf)), decreasing=TRUE))
  #layout(matrix(c(1, 2), nrow=2), heights=c(1, 4))
  #par(mar=rep(0, 4))
  plot.new()
  #text(x=0.5, y=0.5, paste("Wordcloud for : ","India", sep = ""))
  return(wordcloud(rownames(freq), freq[,1], max.words=30,scale=c(2,.2), colors=brewer.pal(1, "Dark2"),main="Title")) 
}

  tweet_Data <- reactive({
    # Change when the "update" button is pressed...
      if (input$update == 0)
      return()
    # ...but not for anything else
    get_tweet_data(input$hashtag, input$tws, FALSE)
  })

        output$word_cloud <- renderPlot({
          if (input$update == 0)
          return()

        tweets= tweet_Data()
        get_word_cloud(tweets)

      })

})#end of Server file

ui.R

library(shiny) library(plotly) library(rtweet) library(ggplot2) library(tm) library(wordcloud) library(wordcloud2) library(dplyr) library(tidyr) library(httpuv)

ui <- shinyUI( pageWithSidebar( headerPanel("Twitter Analysis"),

loading message

sidebarPanel( tags$head(tags$style(type="text/css", "

loadmessage {

           position: fixed;
           top: 0px;
           left: 0px;
           width: 100%;
           padding: 5px 0px 5px 0px;
           text-align: center;
           font-weight: bold;
           font-size: 100%;
           color: #000000;
           background-color: #CCFF66;
           z-index: 105;
         }
      ")),
   conditionalPanel(condition="$('html').hasClass('shiny-busy')",
                    tags$div("Loading...",id="loadmessage")),
   #side panel 1
    conditionalPanel(condition="input.tabselected==1",
                     helpText("This app uses a simplified model of text representation where word order 
                     and grammar are ignored but frequency is not, effectively treating the corpus 
                     like a bag of all of its words together."),

       actionButton("login", "Login to Twitter First"),
       textInput(inputId = "hashtag",
                  label = "Search a Hashtag:"),
               hr(),
       sliderInput("tws", "Maximum number of tweets to obtain:", 
                   min=200, max= 5000, value=25, step = 10),
      br(),
      actionButton("update", "Gather data"),
      br(),

      #uiOutput("Box1"),
      br(),br(),
      actionButton("newcloud", "Generate Wordcloud"))),

  #conditional main panel
  mainPanel(
    tabsetPanel(
       tabPanel("Word Cloud",  plotOutput('word_cloud'), value = 1,  
       conditionalPanel(condition="input.tabselected==1")),
       id = "tabselected")
    )

    )

)

rtweet version

0.7.0

copy/paste output

packageVersion("rtweet")


### Session info

<!-- run the code below and copy/paste the output -->

``` r
## copy/paste output
sessionInfo()

Token

## copy/paste output
rtweet::get_token()
ronaldocpontes commented 4 years ago

I am having the same issue

llrs commented 3 years ago

Sorry couldn't understand what part of your application produces the error you mention. Please try to remove code that is not related to rtweet, so that I can focus on what might be the problem. Closing, now feel free to comment with a reproducible example