【问题标题】:object of type 'closure' is not subsettable: R shiny App“闭包”类型的对象不可子集:R shiny App
【发布时间】:2016-10-21 21:35:25
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

    标签: r graph shiny


    【解决方案1】:

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

    代替:

    subset(selectedData, route_id == input$route)

    尝试使用:

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

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

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

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

    插入

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

    仅供参考

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

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

    【讨论】:

      猜你喜欢
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 2017-03-30
      相关资源
      最近更新 更多