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.
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.
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)