【问题标题】:Shiny error in incorrect number of dimensions尺寸不正确的闪亮错误
【发布时间】:2018-04-10 01:52:52
【问题描述】:

我正在从公共页面的 Facebook API 中提取数据。

我的数据如下所示:

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

根据这些数据,我想构建一个闪亮的应用程序。

应用程序应该采用单个输入 - selectInput(列库中包含的唯一级别),根据用户在应用程序的服务器部分提供的输入进行过滤,并返回一个输出 Facebook 帖子数量的条形图给定的FB页面的一年。

此尝试如下:

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)

但我收到以下错误:

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

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你最好使用reactive() 而不是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")
                  }) 
    

    【讨论】:

    • 是的。这就是问题所在。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多