dsn084
dsn084

Reputation: 88

Shiny - getting correct value from dynamically populated selectInput box

I've tried to generalize a problem with a viz I'm building below, which is that I need to dynamically populate the selectInput box with values from a data frame that will change over time.

I've managed to get my selectInput box to show the correct values, however when I try to show the selected value I'm getting the entire "colour" = "colour" string:

enter image description here

library(shiny)

colour <- data.frame(colourName = c("Blue", "Green", "Red", "Green", "Red", "Purple","Orange"))

ui <- fluidPage(
  htmlOutput("selectUI"),
  textOutput("selection")
)

server <- function(input, output){
  output$selectUI <- renderUI({
    lvl.colour <- levels(colour$colourName)

    selectInput(inputId = "colourSelect",
                label = "Select colour",
                sapply(lvl.colour, function(x) paste0(shQuote(x)," = ",shQuote(x))))
  })

  output$selection <- renderText({
    input$colourSelect
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 92

Answers (1)

Rahul Agarwal
Rahul Agarwal

Reputation: 4100

library(shinydashboard)
library(shiny)

colour <- data.frame(colourName = c("Blue", "Green", "Red", "Green", "Red", "Purple","Orange"))

ui <- fluidPage(
  selectInput("SelectUI", h5("Choose a colour"),colour,colour[1]),
  textOutput("selection")
)

server <- function(input, output){

  output$selection <- renderText({
    input$SelectUI
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions