基于 Dataframe 列创建包含选项和子选项的pickerInput

wpcxdonn  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(141)

我有下面的 Dataframe ,我想创建一个pickerInput(),将Dimension作为主选项,将Subdimension作为子选项,如下所示:

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

在此 Dataframe 中,我只有Environmental,但它们可能更多

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

lsmepo6l1#

您可以使用split创建列表/子列表:

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

相关问题