Setting shiny selectInput from data.frame

Viewed 14879

I am wanting to set the choices within selectInput using a data frame. Below I have an example of it working, and this gives the choices "Peter", "Bill" and "Bob", but I would like these to be the label, and to set the value from L1Data$ID_1. Something that would normally be coded up using:

    selectInput("partnerName", "Select your choice",  c("Peter" = "15","Bob" = "25","Bill" = "30" ) )

Any suggestions for getting this functionality?

ui.R

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Test App"),
  sidebarPanel(
  sliderInput("obs", "Number of observations:", min = 1, max = 1000, value = 500),

  htmlOutput("selectUI")
  ),
 mainPanel(
    plotOutput("distPlot")
  )
))

server.R

library(shiny)
ID_1 <- c(15,25,30,30)
Desc_1 <- c("Peter","Bob","Bill","Bill")
L1Data <- data.frame(ID_1,Desc_1)
shinyServer(function(input, output) {
  output$distPlot <- renderPlot({
    dist <- rnorm(input$obs)
    hist(dist)
  })
  output$selectUI <- renderUI({ 
    selectInput("partnerName", "Select your choice",  unique(L1Data$Desc_1) )
    })
})
1 Answers
Related