Shiny dropdown input (pickerInput or similar): Group responses and return the selected choice and it's group

Hi,

I would like to have a drop down input where the options are grouped by the values of another column. I have found pickerInput can do this by using a lists of options in the choices argument.

However the problem is different groups can have the same choices, if a user selects a choice under one group they would assume that they were selecting that choice within that group, but as the pickerInput only returns the choice and not the group the choice is contained within, I can't find a way to capture than information. Ultimately I would like it to return a concatenated group~choice value which I could use to filter my data (by making a corresponding concatenated field)

Can the 'group' information be returned by pickerInput, are there alternative input types that can do this?

Here is an example of what I have so far

if (interactive()) {
  library(shiny)
  library(shinyWidgets)
  
  
  Group = c(
    "Group1",
    "Group1",
    "Group1",
    "Group1",
    "Group2",
    "Group2",
    "Group2",
    "Group2",
    "Group3"
  )
  Choice = c(
    "Choice1",
    "Choice2",
    "Choice3",
    "Choice4",
    "Choice1",
    "Choice2",
    "Choice3",
    "Choice4",
    "Choice5"
  )
  
  df <- data.frame(Group, Choice)
  df <- lapply(split(df$Choice, df$Group), as.list)
  
  ui <- fluidPage(
    pickerInput(
      inputId = "groups",
      choices = df,
      multiple = TRUE
    ),
    verbatimTextOutput(outputId = "res_grp")
  )
  
  server <- function(input, output) {
    output$res_grp <- renderPrint(input$groups)
    
  }
  
  shinyApp(ui, server)
  
}

Any help would be much appreciated

many thanks

I tested that the following works:

df <- data.frame(Group, Choice) %>% mutate(name=paste0(Group,Choice))
vals <- split(df$Choice,df$Group)
nams <- split(df$name,df$Group)

list_of_choices <- map2(nams,vals,~{names(.x)<-.y;.x}) 

and

  pickerInput(
    inputId = "groups",
    choices = list_of_choices,
    multiple = TRUE
  )
1 Like

Great. thank you very much, that does the trick.

I managed to get something else working too, but yours is much neater. Including it here in case useful to anyone searches for the same in future. Has a pickerInput and selectInput version


Group = c(
  "Group1",
  "Group1",
  "Group1",
  "Group1",
  "Group2",
  "Group2",
  "Group2",
  "Group2",
  "Group3"
)
Choice = c(
  "Choice1",
  "Choice2",
  "Choice3",
  "Choice4",
  "Choice1",
  "Choice2",
  "Choice3",
  "Choice4",
  "Choice5"
)

df <- data.frame(Group, Choice)
df <- (split(df$Choice, df$Group))


addKeys = function(nested_list){
  keyed_nl = list()
  for (group in names(nested_list))
    for (choice in (nested_list[[group]]))
      keyed_nl[[group]][[choice]] = paste0(group, "-", choice)
    keyed_nl
}

ListOfItemsWithNames = df 

keyedList = addKeys(ListOfItemsWithNames)

library(shiny)

shinyApp(    
  fluidPage(
    selectInput("selectInput", "selectInput", keyedList,multiple = TRUE,selectize=TRUE),
    textOutput('selectInput'),
    pickerInput("pickerInput","pickerInput", choices=keyedList,
                options = list(`actions-box` = TRUE),multiple = T),
     textOutput('pickerInput')
  ),
  function(input, output, session){
    output$selectInput = renderText(input$selectInput)
  output$pickerInput = renderText(input$pickerInput)
  }
  
)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.