Shiny 和 Twitter 示例 [英] Shiny and twitter example

查看:21
本文介绍了Shiny 和 Twitter 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个示例来使用 Shiny 处理 R 中的推文.我正在使用 这个页面,但我没有得到任何输出.

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

我已经编辑了 server.r 文件的代码,因为某些命令已被弃用,如下所示:

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?

谢谢

推荐答案

我设法让它发挥作用.这是代码.我希望它可以帮助某人

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

这篇关于Shiny 和 Twitter 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆