Shiny and twitter example

2019-07-17 18:05发布

I'm trying to run a example to process tweets in R with Shiny. I'm using the example in this page, but I'm not getting any output.

The code that I'm using is as follows (which I've corrected from the page as it had some errors with parenthesis, inverted comas, etc.):

ui.r

library(shiny)
shinyUI(pageWithSidebar(
  # Application title
  headerPanel('Tweets hunter'),
  sidebarPanel( textInput('term', 'Enter a term', ''),
                numericInput('cant', 'Select a number of tweets',1,0,200),
                radioButtons('lang','Select the language',c(
                  'English'='en',
                  'Castellano'='es',
                  'Deutsch'='de')),
                submitButton(text='Run')),
  mainPanel(
    h4('Last 5 Tweets'),
    tableOutput('table'),
    plotOutput('wordcl'))
))

server.r

library(shiny)
library(twitteR)
library(wordcloud)
library(tm)
shinyServer(function (input, output) {
  rawData <- reactive(function(){
    tweets <- searchTwitter(input$term, n=input$cant,lang=input$lang)
    twListToDF(tweets)
  })
  output$table <- reactiveTable(function () {
    head(rawData()[1],n=5)
  })
  output$wordcl<- reactivePlot(function(){
    tw.text<-enc2native(rawData()$text,
                        tw.text <- tolower(tw.text),
                        tw.text <- removeWords(tw.text,c(stopwords(input$lang),'rt')),
                        tw.text <- removePunctuation(tw.text,TRUE),
                        tw.text <-unlist(strsplit(tw.text,' ')),
                        word<- sort(table(tw.text),TRUE),
                        wordc<-head(word,n=15),
                        wordcloud(names(wordc),wordc,random.color=TRUE,colors=rainbow(10),scale=c(15,2))
    )
  })
})

I've edited the code of the server.r file as some of the commands are deprecated as follows:

library(shiny)
library(twitteR)
library(wordcloud)
library(tm)
shinyServer(function (input, output) {
  rawData <- reactive(function(){
    tweets <- searchTwitter(input$term, n=input$cant,lang=input$lang)
    twListToDF(tweets)
  })
  #output$table <- reactiveTable(function () {
  #  head(rawData()[1],n=5)
  #})
  output$filetable <- renderTable( 
    { if (is.null(input$files)) { # User has not uploaded a file yet 
      return(NULL) } 
      head(rawData()[1],n=5) })

  #http://shiny.rstudio.com/reference/shiny/latest/renderPlot.html
  #  output$wordcl<- reactivePlot(function(){
  #  tw.text<-enc2native(rawData()$text,
  #                      tw.text <- tolower(tw.text),
  #                      tw.text <- removeWords(tw.text,c(stopwords(input$lang),'rt')),
  #                      tw.text <- removePunctuation(tw.text,TRUE),
  #                      tw.text <-unlist(strsplit(tw.text,' ')),
  #                      word<- sort(table(tw.text),TRUE),
  #                      wordc<-head(word,n=15),
  #                      wordcloud(names(wordc),wordc,random.color=TRUE,colors=rainbow(10),scale=c(15,2))
  #  )
  #})

  output$wordcl<- renderPlot(
      function(){
        tw.text<-enc2native(rawData()$text)
                            tw.text <- tolower(tw.text)
                            tw.text <- removeWords(tw.text,c(stopwords(input$lang),'rt'))
                            tw.text <- removePunctuation(tw.text,TRUE)
                            tw.text <-unlist(strsplit(tw.text,' '))
                            word<- sort(table(tw.text),TRUE)
                            wordc<-head(word,n=15)
                            wordcloud(names(wordc),wordc,random.color=TRUE,colors=rainbow(10),scale=c(15,2))
      }
                            ,width = "auto", height = "auto", res = 72, 
               env = parent.frame(), quoted = FALSE, execOnResize = FALSE,
               outputArgs = list())

})

But I'm not getting any output

Any ideas what is causing I'm missing?

Thanks

1条回答
成全新的幸福
2楼-- · 2019-07-17 19:01

I managed to make it work. Here is the code. I hope it can help someone

library(shiny)
library(twitteR)
library(wordcloud)
library(tm)
shinyServer(function (input, output) {
  rawData <- reactive(
    { tweets <- searchTwitter(input$term, n=input$cant,lang=input$lang)
    return(twListToDF(tweets))
  })

  output$tablel <- renderTable( {
      head(rawData()[1],n=5)
    })

  output$wordcl<- renderPlot(
      {
        tw.text <- rawData()$text
        tw.text <- enc2native(rawData()$text)
        tw.text <- tolower(tw.text)
        tw.text <- removeWords(tw.text,c(stopwords('en'),'rt'))
        tw.text <- removePunctuation(tw.text,TRUE)
        tw.text <- unlist(strsplit(tw.text,' '))
        word <- sort(table(tw.text),TRUE)
        wordc <- head(word,n=15)
        wordcloud(names(wordc),wordc,random.color=TRUE,colors=rainbow(10),scale=c(5,2),min.freq=1)
      }
  )
})
查看更多
登录 后发表回答