在SelectInput(RShiny)中动态填充分组的选项列表

时间:2017-09-18 23:31:20

标签: r shiny

我想创建一个selectInput,其中包含基于我加载的数据框的选项。应将这些选项分组,如下例所示。我知道如何在完全写出时这样做,但是,我希望下拉列表是自动的,以便在我更改数据框时自动更改。我可能会更改组列表或每个组内的指标列表。

conda install -c conda-forge ipywidgets

jupyter nbextension enable --py widgetsnbextension

这是我追求的风格:

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

1 个答案:

答案 0 :(得分: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){
    }
  )
)

-output

enter image description here