我希望通过使用数据框来设置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) )
})
})
factor(unique(L1Data$Desc_1),labels=<...>, levels=<....>)
。 - Ananta