2015-10-12 13 views
7

Tôi có hai số selectInput s và tôi muốn lựa chọn trong phần đầu tiên (Thương hiệu) để thay đổi các lựa chọn có thể có trong phần thứ hai (Kẹo). Vì vậy, ví dụ: nếu ai đó đã chọn "Nestle" trong hộp nhập đầu tiên, thì chỉ các thanh kẹo Nestle mới hiển thị trong hộp thứ hai. Bảng dữ liệu của tôi có một cột cho Nhãn hiệu và một cột cho loại thanh kẹo.Lọc một selectInput dựa trên lựa chọn từ một selectInput khác?

Tôi có mã sau để bắt đầu, nhưng điều này cho thấy TẤT CẢ các lựa chọn, bất kể lựa chọn.

selectInput(inputId="brand", 
        label="Brand:", 
        choices=as.character 
        (unique(candyData$Brand)), 
        selected = "Nestle" 
    ), 
    selectInput(inputId="candy", 
       label="Candy:", 
       choices=as.character 
       (unique(candyData$Candy)), 
       selected = "100Grand" 

Bộ dữ liệu trông giống như sau:

Brand  Candy 
Nestle  100Grand 
Netle  Butterfinger 
Nestle  Crunch 
Hershey's KitKat 
Hershey's Reeses 
Hershey's Mounds 
Mars  Snickers 
Mars  Twix 
Mars  M&Ms 

Cập nhật Câu hỏi Làm thế nào để cập nhật các ValueBox trong Bảng điều khiển của tôi dựa trên việc lọc tiếp theo?

output$count <- renderValueBox({ 

    valueBox(
     value = nrow(candyData), 
     subtitle = "Number of Candy Bars", 
     icon = icon("table") 
    ) 
    }) 
+0

Vui lòng bao gồm tập dữ liệu 'candyData' và phần còn lại của mã cho ứng dụng của bạn. – nrussell

+0

nrussell, tập dữ liệu mẫu đã được hiển thị. – Gary

Trả lời

10

Dưới đây là một cách tiếp cận:

library(shiny) 
library(shinydashboard) 
## 
ui <- shinyUI({ 
    sidebarPanel(

    htmlOutput("brand_selector"), 
    htmlOutput("candy_selector")) 

}) 
## 
server <- shinyServer(function(input, output) { 
    candyData <- read.table(
    text = "Brand  Candy 
    Nestle  100Grand 
    Netle  Butterfinger 
    Nestle  Crunch 
    Hershey's KitKat 
    Hershey's Reeses 
    Hershey's Mounds 
    Mars  Snickers 
    Mars  Twix 
    Mars  M&Ms", 
    header = TRUE, 
    stringsAsFactors = FALSE) 

    output$brand_selector <- renderUI({ 

    selectInput(
     inputId = "brand", 
     label = "Brand:", 
     choices = as.character(unique(candyData$Brand)), 
     selected = "Nestle") 

    }) 

    output$candy_selector <- renderUI({ 

    available <- candyData[candyData$Brand == input$brand, "Candy"] 

    selectInput(
     inputId = "candy", 
     label = "Candy:", 
     choices = unique(available), 
     selected = unique(available)[1]) 

    }) 

}) 
## 
shinyApp(ui = ui, server = server) 

Cập nhật:

Bạn có thể thay đổi định nghĩa ui

ui <- shinyUI({ 
    sidebarPanel(

    htmlOutput("brand_selector"), 
    htmlOutput("candy_selector"), 
    valueBoxOutput("count")) 

}) 

và thêm thông tin sau vào server:

output$count <- renderValueBox({ 

    available <- candyData[candyData$Brand == input$brand, ] 

    valueBox(
    value = nrow(available), 
    subtitle = sprintf("Number of %s Candy Bars", input$brand), 
    icon = icon("table")) 

}) 
+2

Điều này làm việc tuyệt vời. Cám ơn rất nhiều! – Gary

+0

nrussell, tôi có thêm một câu hỏi nữa. Ở đầu ứng dụng Sáng bóng của tôi, tôi có một bộ đếm hiển thị tổng số thanh kẹo trong tập dữ liệu. Làm cách nào để bộ đếm này tự động cập nhật dựa trên bộ lọc tiếp theo? Tôi đã đặt mã hiện tại của mình trong bản sửa đổi. – Gary

+0

@Nick Cập nhật - và để tham khảo trong tương lai, bạn nên hỏi một câu hỏi * riêng biệt *, ngay cả khi nó liên quan đến vấn đề ban đầu, để thông tin dễ dàng tìm kiếm hơn bởi những người dùng khác của trang web này. – nrussell

Các vấn đề liên quan