Shiny and twitter example

◇◆丶佛笑我妖孽 提交于 2020-01-15 04:19:12

问题


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:


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


来源:https://stackoverflow.com/questions/42056518/shiny-and-twitter-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!