How to use select input feature without having to specify the variable names of the data set

Hello,

I am trying to select input with the code below, and it works very well for a specific data set where I can I can use the data file labels in the selectInput feature. However, I do not know how to make the feature general so that I can ulpoad any data set and can get input without having to specify the names of all the variables. Also, the feature should allow for any data set with a different number of columns. I would appreciate help with this.

 selectInput("variable2", "Select variable:",
              c(
                 "Height" = "Height",
                 "Girth" = "Girth",
                 "Volume" = "Volume"
                #"cyl" = "cyl",
                #"mpg" = "mpg",
                #"disp" = "disp",
                #"hp"  = "hp",
                #"drat" = "drat",
                #"wt" = "wt",
                #"qsec" = "qsec",
                #"vs" = "vs",
               # "am" = "am",
               # "gear" = "gear",
               # "carb" = "carb"
              )),  

 output$data <- renderTable({
    
    #mtcars[, c(input$variable), drop = FALSE]
    trees[, c(input$variable), drop = FALSE]
  }, rownames = TRUE)
  

The shiny::updateSelectInput() function will let you update/replace the list of choices in a select input. You can put it inside an event observer so that it is triggered when the data set changes.

Thank you very much. Could you please provide an example of an event observer?

Try this small example.

library(shiny)

ui <- fluidPage(
  selectInput("df", "Select a data frame:", choices = data()$results[, "Item"], selected = "mtcars"),
  selectInput("var", "Select a variable:", choices = names(mtcars)),
  h4("Your choice:"),
  textOutput("choice")
)

server <- function(input, output) {
  # Listen for a variable selection and echo it to the text field.
  output$choice <- renderText({
    input$var
  })
  
  # Listen for a change of database and update the selection of variables.
  observeEvent(
    input$df,
    {
      updateSelectInput(inputId = "var", choices = get(input$df) |> names())        
    }
  )
}

# Run the application 
shinyApp(ui = ui, server = server)

Created on 2025-09-24 with reprex v2.1.1

There's also varSelectInput() -- "select variables from a data frame",
it's used in a demo on https://shiny.posit.co/ landing page.

For your use case and with updateVarSelectInput(), perhaps something like this:

library(shiny)
library(bslib)

datasets <- list(mtcars = mtcars, trees = trees, penguins = penguins)

ui <- page_sidebar(
  sidebar = sidebar(
    radioButtons("inDs", "Select dataset:", choices = names(datasets)),
    varSelectInput("inVar", "Select variable:", datasets[[1]])
  ),
  tableOutput("outData")
)

server <- function(input, output, session) {
  ds <- reactive( datasets[[input$inDs]] )
  
  observe(updateVarSelectInput(session, "inVar", data = ds()))
  
  output$outData <- renderTable({ 
    var_char <- as.character(input$inVar)
    # deal with race conditions,  input$inVar might still include 
    # a column name from previously selected dataset
    req(var_char %in% names(ds()))
    ds()[1:12, var_char, drop = FALSE] 
  }, align = "l")
}

shinyApp(ui, server)
Screen recording

chrome_jMDqWmqDsi

Thank you very much for your help both prubin and margusi.

This topic was automatically closed 90 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.