问题
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