Dynamically populate a grouped list of choices in SelectInput (RShiny)

只谈情不闲聊 提交于 2021-02-08 07:40:49

问题


I would like to create a selectInput with options based on a data frame I load. These options should be grouped, as in the example below. I know how to do it when writing out in full, however, I want the dropdown list be be automated so it changes automatically when I change the data frame. I might change the list of groups, or the list of indicators withinin each group.

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

This is the style I am after:

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){
    }
  )
)

回答1:


One option would be to be split the 'Indicator' by 'group' column

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



来源:https://stackoverflow.com/questions/46289590/dynamically-populate-a-grouped-list-of-choices-in-selectinput-rshiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!