【发布时间】:2014-01-20 03:04:48
【问题描述】:
我想使用数据框在 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")
)
))
服务器.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) )
})
})
【问题讨论】:
-
试试
factor(unique(L1Data$Desc_1),labels=<...>, levels=<....>)