Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the 'shiny' R package deal with data frames?

I am building a web app that downloads tweets using the 'twitteR' R package, munging those tweets and displaying them via a 'shiny' R web app. I have no problem executing the code that downloads and processes the tweets into a data frame:

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

... you can run this in your terminal (with the twitteR library loaded) yourself and see that it downloads the tweet data and prints the resulting data frame to the screen.

But, when I use this sort of call in a 'shiny' app (server-side)... for example...


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

... I get the following error:

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

... all I want to do is be able to change the user whose tweets are being downloaded and munged, then output the resulting data frame (... the datasetInput() return, loaded to output$view) to the mainPanel(). I have no idea why this is not working.

Any help would be great!

like image 676
user1854990 Avatar asked Nov 27 '12 00:11

user1854990


1 Answers

I think I've got it: https://github.com/rstudio/shiny/commit/0b469f09df7e2ca3bbdb2ddadc8473a8126a9431

Until this is properly tested and rolled into a new Shiny build, you can test it by using devtools to install straight from GitHub:

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

Thanks, glad to have that one fixed!

like image 167
Joe Cheng Avatar answered Oct 16 '22 20:10

Joe Cheng