Search code examples
rlistshinyshinyappsselectinput

How can I create a selectInput only with the names of a list? (without showing the content of the list) Shiny


I have 3 vectors:

colors = c("blue", "red", "green", "yellow", "white", "black")
numbers = c("1", "2", "3", "4")
things = c("phone", "fridge", "computer", "chair", "table", "notebook", "bag", "jacket")

In order to have all of them in a list, I joined them into a list.

list_options = list("colors" = colors, "numbers" = numbers, "things" = things)

My main objective is to create a Shiny App which has a SelectInput only with the names of the list ("colors", "numbers", "things"). However, if you click in one of the options,you will be able to have the values of the list in order to use them later.

For example, if you select "things", I want to see all the variables inside of the vector (phone, fridge, computer...).

Is there a way to do it? Now, with my code the app shows the name of each vector with his options and gives you the option that you have selected.

image

This is how I want to see my selectInput in the app.

image2

However, as I said, I want to use the content of each vector later (for that in my example I have written a "renderText"). If you select "colors" you will see "blue, red, green, yellow...".

This is my code:

colors = c("blue", "red", "green", "yellow", "white", "black")
numbers = c("1", "2", "3", "4")
things = c("phone", "fridge", "computer", "chair", "table", "notebook", "bag", "jacket")
    
list_options = list("colors" = colors, "numbers" = numbers, "things" = things)


if (interactive()) {
        
  shinyApp(
    ui = fluidPage(
      selectInput("option", "Options:", list_options),
      textOutput("text")
    ),
    server = function(input, output) {
      output$text <- renderPrint({
        input$option
      })
    }
  )
}

Thanks very much in advance,


Solution

  • I'm not sure to understand. Is it what you want:

      shinyApp(
        ui = fluidPage(
          selectInput("option", "Options:", names(list_options)),
          textOutput("text")
        ),
        server = function(input, output) {
          output$text <- renderPrint({
            list_options[[input$option]]
          })
        }
      )