2012-11-27 46 views
11

我正在構建一個網絡應用程序,它使用'twitteR'R軟件包下載推文,將這些推文進行整理並通過'閃亮'R Web應用程序進行顯示。我有執行該下載和處理的tweet成數據幀的代碼沒有問題:'閃亮'R包如何處理數據幀?

do.call('rbind', lapply(userTimeline('nutwition_log'), as.data.frame)) 

...你可以在你的終端(與加載Twitter的庫)運行這個自己,看看它下載鳴叫數據並將結果數據幀打印到屏幕上。

但是,當我使用這種call的 '閃亮' 的應用程序(服務器端)...例如...


server.R:

library(shiny) 
library(twitteR) 
shinyServer(function(input, output) { 

    datasetInput <- reactive(function() { 
    tweets <- userTimeline(input$subscriber) 
    do.call('rbind', lapply(tweets, as.data.frame)) 
    }) 

    output$view <- reactiveTable(function() { 
    head(datasetInput(), n = input$obs) 
    }) 

}) 

ui.R:

library(shiny) 
library(twitteR) 

shinyUI(pageWithSidebar(
    headerPanel('FitnessTrack'), 
    sidebarPanel(
    selectInput("subscriber", "Select Subscriber:", 
       choices = c("nutwition_log", "anotherAccount")), 
    numericInput("obs", "Number of observations to view:", 10) 
), 
    mainPanel(
    tableOutput("view") 
) 
)) 

...我收到以下錯誤:

Error in as.data.frame.default(X[[1L]], ...) : 
    cannot coerce class 'structure("status", package = "twitteR")' into a data.frame 
Error in as.data.frame.default(X[[1L]], ...) : 
    cannot coerce class 'structure("status", package = "twitteR")' into a data.frame 
Error in as.data.frame.default(X[[1L]], ...) : 
    cannot coerce class 'structure("status", package = "twitteR")' into a data.frame 

...所有我想要做的是能夠改變其微博被下載和被改寫的用戶,然後輸出產生的數據幀(...的datasetInput()回報,裝output$view)到mainPanel()。我不知道爲什麼這不起作用。

任何幫助將是偉大的!

+1

看起來像一個錯誤。無論出於何種原因,在調用庫(twitteR)時,as.data.frame的方法都不會被複制,因此您應該在http://groups.google.com/group/shiny-discuss報告。甚至可能是一個命名空間問題。當我調整你的代碼來使用'twitteR :: as.data.frame'時,它會產生新的錯誤。 –

+1

這不是一個閃亮的bug。 'as.data.frame(userTimeline(「nutwition_log」))' – GSee

+1

同樣的錯誤雖然奇怪,因爲那不是他正在運行的代碼。但它就像他一樣對待它。他正在運行'lapply(userTimeline(「nutwition_log」),as.data.frame)' –

回答

6

我不確定這是否是一個錯誤,但肯定會有一些奇怪的事情發生在這裏,Joe Cheng和co。想知道。它的工作原理,是這樣的:

server.R

library(shiny) 
library(twitteR) 
shinyServer(function(input, output) { 

    datasetInput <- reactive(function() { 
    tweets <- userTimeline(input$subscriber) 
    tmp <- lapply(1:length(tweets),function(x) data.frame(
     text=tweets[[x]]$text, 
     created=tweets[[x]]$created, 
     screename=tweets[[x]]$getScreenName())) 

    do.call(rbind,tmp) 
    }) 

    output$view <- reactiveTable(function() { 
    head(datasetInput(), n = input$obs) 
    }) 

}) 

因此,這不是與data.frames問題,而是事做,Twitter的設置參考類status的對象的方法方式。通過訪問器引用字段來運行完全相同的代碼似乎運行得很好。

感覺像「又一個S4 /參考類神祕」。

10

我想我知道了:https://github.com/rstudio/shiny/commit/0b469f09df7e2ca3bbdb2ddadc8473a8126a9431

直到這是很好的測試,並捲成新的閃亮的版本,您可以通過使用devtools以從GitHub直安裝測試:

library(devtools) 
install_github('shiny', 'rstudio') 

謝謝,很高興有一個固定!

+0

我確信這只是我剛剛編寫錯誤代碼的一個例子。我覺得有點激動,我可以幫忙。看到新的「下載數據」部分。感謝一堆爲了解決這個問題! – user1854990