簡體   English   中英

從data.frame設置閃亮的selectInput

[英]Setting shiny selectInput from data.frame

我想使用數據框在selectInput內設置選擇。 下面我有一個工作示例,它給出了選項“ Peter”,“ Bill”和“ Bob”,但是我希望它們成為標簽,並從L1Data $ ID_1設置值。 通常使用以下代碼進行編碼:

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

關於獲得此功能的任何建議?

用戶界面

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

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

服務器

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

您應該創建一個命名向量來設置selectInput函數的choices參數

choices = setNames(L1Data$ID_1,L1Data$Desc_1)
si <- selectInput("partnerName", "Select your choice",  choices)

您可以檢查結果:

cat(as.character(si))
<label class="control-label" for="partnerName">Select your choice</label>
<select id="partnerName">
  <option value="15" selected="selected">15</option>
  <option value="25">25</option>
  <option value="30">30</option>
  <option value="30">30</option>
</select>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM