闪亮 - 从动态填充的selectInput框中获取正确的值

时间:2017-11-21 04:56:42

标签: r shiny

我试图用下面构建的一个问题概括一个问题,这就是我需要用一个随时间变化的数据框中的值动态填充selectInput框。

我设法让我的selectInput框显示正确的值,但是当我尝试显示所选的值时,我得到整个“颜色”=“颜色< / em>“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)

1 个答案:

答案 0 :(得分:1)

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)