Shiny selectinput with named list (duplicates an extracting both parent and child elements)

Hi, is there a way of having items with the same name as in a shinyInput?

  • In the example below "A" is in both Letters and Random but is differentiated by the parent. But this does not work in the shinyInput right now. How can this be fixed?
  • Also, is it possible to work out what the parent is (would need to sort out the first problem), e.g. which parent did "A" come from?
library(shiny)

test_list <- list(Letters = c("A", "B", "C"),
                  Numbers = c("1", "2", "3"),
                  Random = c("A", "2", "C"))

shinyApp(
  ui = fluidPage(
    selectInput("selection",
                label = "Choose:",
                choices = test_list,
                selected = test_list[1]),
    textOutput("result")
  ),
  server = function(input, output) {
    output$result <- renderText({
      paste("You chose", input$selection)
    })
  }
)

image

We could work with named vectors / lists:

library(shiny)

test_list <- list(Letters = c("A", "B", "C"),
                  Numbers = c("1", "2", "3"),
                  Random = c("A", "2", "C"))

modified_list <- setNames(lapply(names(test_list), function(x){
  setNames(paste0(x, "_", test_list[[x]]), test_list[[x]])
}), names(test_list))

shinyApp(
  ui = fluidPage(
    selectInput("selection",
                label = "Choose:",
                choices = modified_list,
                selected = modified_list[1]),
    textOutput("result")
  ),
  server = function(input, output) {
    output$result <- renderText({
      paste("You chose", input$selection)
    })
  }
)

As alternatives please check:

Thanks. Not quite what I was after (I had tried the modified lists) but the other bits like jsTreeR are useful.

It seems to do exactly what you asked / where is lacking ? how might it be improved

1 Like

I suppose it is what I was after (as in what I had written) but I already had this solution and what I was really after is to have two outputs from the one input (e.g. the parent and child as separate objects).

But also, not related to this is that the problem that I was having wasn't due to the inputs but to some other issue (my fault).

Given it answers the question, I'll mark it as completed then.

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.