Create a pickerInput with choices and subchoices based on dataframe columns

216 views Asked by At

I have the dataframe below and I want to create a pickerInput() with Dimension as the main choices and Subdimension as sub-choices like:

pickerInput(
    inputId = "p1",
    label = "Select all option",
    choices = list(Environmental = list("A","B","C"),
                   EnvironmentalB = list("A"),
                   EnvironmentalC = list("A","B")),
    multiple = TRUE,
    options = list(`actions-box` = TRUE)
  )

In this dataframe I have only Environmental but they may be more

df<-structure(list(Dimension = c("environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental", 
"environmental", "environmental", "environmental", "environmental"
), Subdimension = c("A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C")), row.names = c(NA, -42L), class = c("tbl_df", 
"tbl", "data.frame"))
1

There are 1 answers

5
Waldi On BEST ANSWER

You could create the list / sublist using split:

pickerInput(
  inputId = "p1",
  label = "Select all option",
  choices = split(unique(df)$Subdimension,unique(df)$Dimension),
  multiple = TRUE,
  options = list(`actions-box` = TRUE)
)

enter image description here