2017-09-18 33 views
1

我想根據我加載的數據框創建帶有選項的selectInput。這些選項應該分組,如下例所示。我知道如何在完全寫出時做到這一點,但是,我希望下拉列表能夠自動化,以便在更改數據框時自動更改。我可能會更改組的列表或每個組內的指標列表。在SelectInput(RShiny)中動態填充選項的分組列表

DD <- data.frame(group=c("Diagnosis","Diagnosis", "Treatment", "Treatment", "Outcome", "Outcome", "Outcome"), 
       Indicator=LETTERS[1:7]) 


> DD 
     group Indicator 
1 Diagnosis   A 
2 Diagnosis   B 
3 Treatment   C 
4 Treatment   D 
5 Outcome   E 
6 Outcome   F 
7 Outcome   G 

這是我找的風格:

runApp(
    list(
    ui = fluidPage(
     sidebarLayout(
     sidebarPanel(
      selectInput(inputId = "IND", 
         label = "Select indicator:", 
         choices = list("Diagnosis" = c("A", "B"), 
             "Treatment" = c("C", "D"), 
             "Outcome" = c("E", "F", "G"))) 
      , width = 3), 

     mainPanel(
     ) 
     ) 
    ) 


    , server = function(input, output, session){ 
    } 
) 
) 

回答

2

一個選項將是split的 '指標' 由 '組' 列

library(shiny) 
DD <- data.frame(group=c("Diagnosis","Diagnosis", "Treatment", 
     "Treatment", "Outcome", "Outcome", "Outcome"), 
      Indicator=LETTERS[1:7]) 
runApp(
    list(
    ui = fluidPage(
     sidebarLayout(
     sidebarPanel(
      selectInput(inputId = "IND", 
         label = "Select indicator:", 
         choice = split(DD$Indicator, DD$group)) 
      , width = 3), 

     mainPanel(
     ) 
    ) 
    ) 


    , server = function(input, output, session){ 
    } 
) 
) 

- 輸出

enter image description here

+1

真棒簡單的解決方案!大! – Luc