gpt4 book ai didi

r - 从data.frame设置 Shiny 的selectInput

转载 作者:行者123 更新时间:2023-12-04 10:55:35 25 4
gpt4 key购买 nike

我想使用数据框在selectInput内设置选择。下面我有一个工作示例,它给出了选项“ Peter”,“ Bill”和“ Bob”,但是我希望它们成为标签,并从L1Data $ ID_1设置值。通常使用以下代码进行编码:

    selectInput("partnerName", "Select your choice",  c("Peter" = "15","Bob" = "25","Bill" = "30" ) )


关于获得此功能的任何建议?

用户界面

library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Test App"),
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 1, max = 1000, value = 500),

htmlOutput("selectUI")
),
mainPanel(
plotOutput("distPlot")
)
))


服务器

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) )
})
})

最佳答案

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

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>

关于r - 从data.frame设置 Shiny 的selectInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20886800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com