Shiny selectInput to get list from a dataframe column

Is there a way to initialize selectInput to get list from a dataframe column ?

Currently, I'm using code below;

    select_demo <- selectInput(
        "Catgory_Name", 
        "Category Name", 
        c("", 
          "Item1", 
          "Item2", 
          "Item3", 
          "Item4"
        )
    )
1 Like

Yes, but you probably need to do this in the server instead of in the ui definition. So in the UI, use uiOutput, and in the server make a renderUI reactive that creates the selectInput with the chocies parameter populated by the data frame:

# in ui
ui <- fluidPage(
  uiOutput("yourinput")
)

# in server
server <- function(input, output) {
  output$yourinput <- renderUI({
      selectInput(
            "Catgory_Name", 
            "Category Name",
            unique(yourdataframe$column )
  })
}

1 Like

@stkrog thanks for your reply.

Code that you've provided throws some error at the bracket ends.

Hard to help without knowing the error...

yourdataframe <- data.frame(column=1:4)

ui <- shiny::fluidPage(
    shiny::uiOutput("yourinput")
)

server <- function(input, output) {
    output$yourinput <- renderUI({
        shiny::selectInput(
            "Category_Name", 
            "Category Name",
            unique(yourdataframe$column ))
    })
}

shiny::shinyApp(ui,server)

It was just a missing parenthesis in the unique line.


Another option:

You can create an empty selectInput and update it afterwards.


Lastly, another option is if the data is static (i.e. it is not gonna change during the app life), you can load it as global and use it inside the ui.

yourdataframe <- data.frame(column=1:4)

ui <- shiny::fluidPage(
  shiny::selectizeInput("Category_Name", "Category Name", unique(yourdataframe$column )),
  shiny::textOutput("selected")
)

server <- function(input, output) {
  output$selected <- shiny::renderText(as.character(input$Category_Name))
}

shiny::shinyApp(ui,server)
1 Like

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.