设置从data.frame选择输入

设置从data.frame选择输入,r,shiny,R,Shiny,我想使用数据框在selectInput中设置选项。下面我举了一个it工作的例子,它提供了“Peter”、“Bill”和“Bob”选项,但我希望这些是标签,并从L1Data$ID_1设置值。通常使用以下方式编码的内容: selectInput("partnerName", "Select your choice", c("Peter" = "15","Bob" = "25","Bill" = "30" ) ) 有没有关于获取此功能的建议 用户界面 服务器.R library(shiny

我想使用数据框在selectInput中设置选项。下面我举了一个it工作的例子,它提供了“Peter”、“Bill”和“Bob”选项,但我希望这些是标签,并从L1Data$ID_1设置值。通常使用以下方式编码的内容:

    selectInput("partnerName", "Select your choice",  c("Peter" = "15","Bob" = "25","Bill" = "30" ) )
有没有关于获取此功能的建议

用户界面

服务器.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) )
    })
})
库(闪亮)

ID_1您应该创建一个命名向量来设置
selectInput
函数的choices参数

choices = setNames(L1Data$ID_1,L1Data$Desc_1)
si <- selectInput("partnerName", "Select your choice",  choices)
choices=setNames(L1Data$ID\u 1,L1Data$Desc\u 1)

si try
factor(unique(L1Data$Desc_1),labels=,levels=)
我认为现在可以了,但我想了解更多关于如何检查结果的信息。我还没有在Shinny中使用普通的HTML代码,您能解释一下我应该把它放在哪里(在ui.R或server.R中)。谢谢
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>