【问题标题】:Setting shiny selectInput from data.frame从 data.frame 设置闪亮的 selectInput
【发布时间】: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=&lt;...&gt;, levels=&lt;....&gt;)

标签: r shiny


【解决方案1】:

您应该创建一个命名向量来设置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>

【讨论】:

  • 我认为现在可行,但我想了解更多有关如何检查结果的信息。我没有通过闪亮使用普通的 HTML 代码,你能解释一下我应该把它放在哪里(在 ui.R 或 server.R 中)。谢谢
猜你喜欢
  • 2016-07-28
  • 2019-01-24
  • 2015-05-07
  • 1970-01-01
  • 2021-09-23
  • 2020-08-02
  • 2020-08-24
  • 2016-10-08
  • 2019-03-20
相关资源
最近更新 更多