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,

2

There are 2 best solutions below

0
On BEST ANSWER

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]]
      })
    }
  )
1
On

More convoluted than Stephane's answer: using Observe() and updateSelectInput()

library(shiny)

values1 <- paste(c(1, 2, 3))
values_list <- list('1' = c(4, 5, 6), '2' = 7:9, '3' = 10:12)

ui <- fluidPage(

    # Application title
    titlePanel("Testing a dynamic dropdown"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput("value",
                        "Choose value",
                        values1,
                        selected = '1', multiple = F),
            
            selectInput("listed_values",
                        "Choose subvalue",
                        values_list['1'],   # your default starting values
                         multiple = F)
        ),
    
    mainPanel(
       textOutput("text_result")
    )
    
    )
    
)

server <- function(input, output, session) {
    
    
    observe({              # update 2nd dropdown based on first
        updateSelectInput(session, 
                          'listed_values',
                          choices = values_list[input$value],
                          selected = values_list[input$value][1])
    })
    
  
    output$text_result <- renderPrint({       # print selection
        print(paste('You chose', input$listed_values))
    })
}

shinyApp(ui = ui, server = server)