簡體   English   中英

R更新Shiny中的選擇框

[英]R update selection boxes in Shiny

我在工作中遇到了問題。 我正在構建一個Shiny應用程序,並嘗試根據三個選擇框的值來對數據進行子集化。 我的數據有三個變量,專員,社區和實踐。 我需要的是,無論用戶選擇哪個盒子來過濾數據,其他兩個更新只顯示相關的選擇。 加載時的默認值是顯示所有框中的所有選項。

我已經開始了,但沒有快速到達。 下面的代碼顯示了加載專員的所有選擇,但其他兩個沒有。 在“專員”框中選擇選項時,“鄰居”框會更新,然后在選擇“鄰居”時,“實踐”框會更新。

library(shiny)

ui <- fluidPage(
  fluidRow(
    div(style="display: inline-block;vertical-align:top;",
        uiOutput("Box1"),
        uiOutput("Box2"),
        uiOutput("Box3")
    )
  )
)

server <- function(input, output) {
  data <- data.frame(Commissioner = c(rep("CommA", 6), rep("CommB", 6)),
                     Neighbourhood = c(rep("Nhood1", 2), 
                                       rep("Nhood2", 2),
                                       rep("Nhood3", 2),
                                       rep("Nhood4", 2),
                                       rep("Nhood5", 2),
                                       rep("Nhood6",2)),
                     Practice = c("Prac1", "Prac2", "Prac3", "Prac4", "Prac5", 
                                  "Prac6", "Prac7", "Prac8", "Prac9", "Prac10", 
                                  "Prac11", "Prac12")
                     )

  output$Box1 = renderUI(
    selectInput("commissioner",
                  "Select a Commissioner",
                  c("All",as.character(unique(data$Commissioner))),
                  "selectcommissioner")
  )


  output$Box2 = renderUI(
    selectInput("neighbourhood",
                "Select a Neighbourhood",
                c("All",     as.character(unique(data$Neighbourhood[which(data$Commissioner ==     input$commissioner)]))),
                "selectnhood")
    )

  output$Box3 = renderUI(
    selectInput("practice", 
                "Select a Practice", 
                c("All", as.character(unique(data$Practice[which(data$Neighbourhood == input$neighbourhood)]))), 
                "selectpractice")
    )


}

shinyApp(ui = ui, server = server)

我已經修改了你的服務器代碼來實現你想要的。

server <- function(input, output) {
  data <- data.frame(Commissioner = c(rep("CommA", 6), rep("CommB", 6)),
                     Neighbourhood = c(rep("Nhood1", 2), 
                                       rep("Nhood2", 2),
                                       rep("Nhood3", 2),
                                       rep("Nhood4", 2),
                                       rep("Nhood5", 2),
                                       rep("Nhood6",2)),
                     Practice = c("Prac1", "Prac2", "Prac3", "Prac4", "Prac5", 
                                  "Prac6", "Prac7", "Prac8", "Prac9", "Prac10", 
                                  "Prac11", "Prac12")
  )

  output$Box1 = renderUI(
    selectInput("commissioner",
                "Select a Commissioner",
                c("All",as.character(unique(data$Commissioner))),
                "selectcommissioner")
  )


  output$Box2 = renderUI({

    if(!is.null(input$commissioner))
    {
      if(input$commissioner == "All"){
        opts1 <- c("All", as.character(unique(data$Neighbourhood)))
      }else{
        opts1 <-  c("All",as.character(unique(data$Neighbourhood[which(data$Commissioner==input$commissioner)])))
      }
    }
    selectInput("neighbourhood",
                "Select a Neighbourhood",
                opts1,
                "selectnhood")
  })

  output$Box3 = renderUI({


    if(!is.null(input$neighbourhood))
    {
      if(input$neighbourhood == "All"){
        opts2 <- c("All", as.character(unique(data$Practice)))
      }else{
        opts2 <- c("All", as.character(unique(data$Practice[which(data$Neighbourhood == input$neighbourhood)])))
      }
    }
    selectInput("practice", 
                "Select a Practice", 
               opts2, 
                "selectpractice")

  })


}

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM