在Shiny中的selectInput之后过滤sliderInput的数据集?

时间:2016-02-21 19:10:44

标签: r web-applications shiny

我正在处理NCAA运动会收入数据。我希望用户首先选择会议(例如“ACC”或“BigTen”)。然后,我想要弹出一个滑块,显示该会议中学校的最低和最高收入。

我的数据(在名为conf school incomeMillions ACC NorthCarolina 105.1 ACC Duke 110.2 ACC Clemson 94.1 BigTen OhioState 150.9 BigTen Minnesota 67.6 Pac12 California 54.3 Pac12 Oregon 76.5 ... 的表格中)如下所示:

runApp(list(
  ui <- bootstrapPage(
    sidebarLayout(
      sidebarPanel(
        selectInput(inputId = "conference",
                    label = "Choose a conference",
                    choices = unique(colleges$conf),
                    multiple = FALSE)
      )
    ),
    uiOutput("conference")
  ),
  server = function(input, output){
    table <- colleges[conf == input$conf],
    minZ <- round(min(table$incomeMillions), 2),
    maxZ <- round(max(table$incomeMillions), 2),
    output$slider = renderUI({
      mydata = get(input$range)
      sliderInput("slider", h3("Z-score range"), min = minZ, max = maxZ)
    })
  }
))

到目前为止,这是我的代码:

// work out selected date 
            var dateSelect = $(this).datepicker('getDate'); //used below

            var dayOfWeek = $.datepicker.formatDate('DD', dateSelect); //shows Monday
            $('#datepicker-day-of-week').text(dayOfWeek);

我被卡住了。如何动态过滤该数据集,以便用户从特定会议的最低和最高收入中进行选择,而不仅仅是整个数据集?

1 个答案:

答案 0 :(得分:1)

试试这个,其优势在于,因为Shiny很聪明,会缓存data()的输出,这样即使你多次调用它,也只会被评估一次。

library(DT)
library(data.table)

ui <- bootstrapPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "conf",
                  label = "Choose a conference",
                  choices = unique(as.character(colleges$conf)),
                  multiple = FALSE),
      uiOutput("slider")
    ),
    mainPanel(dataTableOutput("display"))
  )
)

server = function(input, output){

  data <- reactive({
    validate(
      need(input$conf, "Please select a conference.")
    )
    colleges[conf == input$conf]
  })

  output$slider = renderUI({
    income <- data()$incomeMillions

    minZ <- round(min(income), 2)
    maxZ <- round(max(income), 2)

    sliderInput("slider", h3("Z-score range"), 
                  min = minZ, max = maxZ, value = minZ)
  })

  output$display <- renderDataTable({
    data()
  })
}

shinyApp(ui = ui, server = server)