对象类型'关闭'不是子集:R闪亮的应用程序

时间:2016-10-21 21:35:26

标签: r graph shiny

我正在尝试使用R shiny创建图形应用程序。因此,我从用户那里获取用户的路线,停止/旅行ID,(登机,下车或加载)的输入。所以ui.r是

ui <- fluidPage(
  pageWithSidebar(
    headerPanel('FAST_TRIPS Visulization', windowTitle = "Fast_trips Visaualization"),
    sidebarPanel(
      selectInput('route', 'Choose the Route No.', unique(y['route_id'])),
      selectInput('id', 'Please Choose Stop or Trip ID', c('stop_id','trip_id')),
      selectInput('rider', 'What do you wanna compare?', c('boarding', 'alighting', 'load')),
      radioButtons('method','Please select your method', c('Sum', 'Average'))),
    mainPanel(

      plotOutput('plot1')

    )
  )
)

然后我尝试提取特定路线和聚合值的数据,例如使用stop_id登机并尝试为那些stop_id创建条形图。 server.R在

之下
server <- function(input, output, session) {

  # Combine the selected variables into a new data frame



  selectedData <- reactive({
    y[c('route_id', input$id, input$rider)]

  })

  data <- reactive({
    subset(selectedData, route_id == input$route)
    })
  a <- reactive({
    aggregate(input$rider~input$id,data,input$method)
    })

  s <- reactive({input$rider})

output$plot1 <- renderPlot({barplot(a[s])})

}

但是我收到以下错误:

Error: object of type 'closure' is not subsettable

请帮我解决这个问题。我是新手。

1 个答案:

答案 0 :(得分:0)

您应该将反应式表达式作为函数访问,因此您需要将()添加到对作为反应式表达式创建的变量的任何调用中。

而不是:

subset(selectedData, route_id == input$route)

尝试使用:

subset(selectedData(), route_id == input$route)

甚至使用额外的变量来避免问题。

selectedData_ <- selectedData()
subset(selectedData_, route_id == input$route)

最后,您不需要将单个input放入反应式表达式中,只需将其用作renderPlot的任何其他反应式表达式。

Insted of

s <- reactive({input$rider})
output$plot1 <- renderPlot({barplot(a[s])})

仅限使用

output$plot1 <- renderPlot({
  a_ <- a()
  barplot(a_[input$rider])
})

请注意,由于您没有为y提供任何代表性数据,因此我无法完全测试您的代码,但此答案应解决关闭错误。