Home > Back-end >  Create a pickerInput with choices and subchoices based on dataframe columns
Create a pickerInput with choices and subchoices based on dataframe columns

Time:12-23

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"))

CodePudding user response:

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

  • Related