我想使用数据框在selectInput中设置选项。下面我有一个工作的例子,这给出了选项“Peter”,“Bill”和“Bob”,但我希望这些是标签,并设置L1Data $ ID_1的值。通常使用以下代码编写的东西:
selectInput("partnerName", "Select your choice", c("Peter" = "15","Bob" = "25","Bill" = "30" ) )
有关获得此功能的任何建议吗?
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) )
})
})
答案 0 :(得分:11)
您应该创建一个命名向量来设置selectInput
函数的选择参数。
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>