0
votes

I am pulling data from the Facebook API for public pages.

My data looks like this:

> str(db)
'data.frame':   3494 obs. of  13 variables:
 $ X             : int  1 2 3 4 5 6 7 8 9 10 ...
 $ from_id       : num  1.06e+14 1.06e+14 1.06e+14 1.06e+14 1.06e+14 ...
 $ from_name     : Factor w/ 4 levels "Halkbank AD Skopje",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ message       : Factor w/ 3162 levels "- Имаш иновативна идеја за бизнис? <ed><U+00A0><U+00BD><ed><U+00B4><U+00A5>\n- Ти фали грант до 10.000€? \n- Мо"| __truncated__,..: 2149 3061 2115 2751 1187 778 786 2848 2885 1748 ...
 $ created_time  : Factor w/ 3491 levels "2012-01-04T15:39:25+0000",..: 3487 3483 3482 3477 3475 3472 3471 3470 3466 3465 ...
 $ type          : Factor w/ 6 levels "event","link",..: 4 4 4 4 4 4 4 4 4 4 ...
 $ link          : Factor w/ 3111 levels "http://6.seo.com/wp-content/uploads/2011/12/How-has-the-internet-changed-education-Infographic.png",..: 1431 1429 1425 1428 1430 1423 1440 1426 1427 1424 ...
 $ id            : Factor w/ 3494 levels "106087736756825_106091843423081",..: 153 151 146 149 152 144 150 147 148 145 ...
 $ story         : Factor w/ 322 levels "Halkbank AD Skopje added 10 new photos — with Ена Поповиќ.",..: NA NA NA NA NA NA 117 NA NA 123 ...
 $ likes_count   : int  19 89 49 36 169 37 107 227 47 29 ...
 $ comments_count: int  0 0 0 5 1 0 3 57 0 0 ...
 $ shares_count  : int  3 1 2 3 7 1 10 18 12 0 ...
 $ bank          : chr  "gs" "gs" "gs" "gs" ...

From this data I want to build a Shiny app.

The app is supposed to take a single input - selectInput (unique levels contained in the column bank), filter based on the input the user provides in the server part of the application and return a barchart that outputs the number of facebook posts a year by a given fb page.

This attempt is below:

library(shiny)
library(lubridate)
library(ggplot2)

setwd("~/r_directory")
db <- read.csv("./banks.csv")
db$bank <- as.character(db$bank)

db$date <- substr(db$created_time, 0, 10) 
db$date <- ymd(db$date)
db$year <- year(db$date)
list <- unique(db$bank)

# Use a fluid Bootstrap layout
ui <- fluidPage(    

  # Give the page a title
  titlePanel("Number of daily FB posts by bank"),

  # Generate a row with a sidebar
  sidebarLayout(      

    # Define the sidebar with one input
    sidebarPanel(
      selectInput("bank", "Bank:", 
                  choices=list),
      hr(),
      helpText("Data info...")
    ),

    # Create a spot for the barplot
    mainPanel(
      plotOutput("bankData")  
    )

  )
)

server <- function(input, output) {

  observe({

  db <- db[input$bank, "bank"]  

  test <-  data.frame(table(db[, "year"]))


  # Fill in the spot we created for a plot
  output$bankData <- renderPlot({


    p <-ggplot(test, aes(Var1, Freq)) 
    p + geom_bar(stat = "identity")



  })  })
}


shinyApp(ui = ui, server = server)

But I am getting the following error:

Warning: Error in [: incorrect number of dimensions
Stack trace (innermost first):
61: table
60: data.frame
59: observerFunc [#7]
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
ERROR: [on_request_read] connection reset by peer
1

1 Answers

1
votes

You'd better use reactive() rather than observe() :

test <-  reactive({
              db <- db[db$bank==input$bank, ]
              data.frame(table(db[, "year"]))
              })

output$bankData <- renderPlot({
              p <-ggplot(test(), aes(Var1, Freq)) 
              p + geom_bar(stat = "identity")
              })