2017-02-05 22 views
2

我想運行一個例子來處理R與閃亮的鳴叫。我正在使用this頁面中的示例,但我沒有收到任何輸出。閃亮和微博的例子

,我正在使用的代碼如下(我已經從網頁修正,因爲它曾與括號,倒昏迷等一些錯誤):

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文件的代碼,一些命令已被棄用如下:

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

}) 

但我沒有得到任何輸出

任何想法是什麼原因造成我失蹤?

謝謝

+2

您是否試圖追蹤錯誤來源?,即檢查反應值並打印它們,或在某些位置添加打印(「此處」)? 你是如何開始調查的? – OmaymaS

+0

我已更正一些棄用的命令(我將編輯以顯示該代碼),但現在我又收到另一個錯誤 – Selrac

回答

2

我設法使它工作。這是代碼。我希望它可以幫助某人

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