gpt4 book ai didi

r - 使用单元素向量从命名列表中选择输入

转载 作者:行者123 更新时间:2023-12-04 09:52:17 26 4
gpt4 key购买 nike

因此,我正在尝试从命名列表中进行选择,这在遵循 selectInput 引用页面时效果很好:

## Only run examples in interactive R sessions
if (interactive()) {

# basic example
shinyApp(
ui = fluidPage(
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
tableOutput("data")
),
server = function(input, output) {
output$data <- renderTable({
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
)

# demoing optgroup support in the `choices` arg
shinyApp(
ui = fluidPage(
selectInput("state", "Choose a state:",
list(`East Coast` = c("NY", "NJ", "CT"),
`West Coast` = c("WA", "OR", "CA"),
`Midwest` = c("MN", "WI", "IA"))
),
textOutput("result")
),
server = function(input, output) {
output$result <- renderText({
paste("You chose", input$state)
})
}
)
}

Standard

但是,如果我使用一个包含命名向量的列表,其中包含单个元素,这会以某种方式改变选择器组织,只显示列表名称,而不显示这些单元素向量包含的内容。
## Only run examples in interactive R sessions
if (interactive()) {

# basic example
shinyApp(
ui = fluidPage(
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
tableOutput("data")
),
server = function(input, output) {
output$data <- renderTable({
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
)

# demoing optgroup support in the `choices` arg
shinyApp(
ui = fluidPage(
selectInput("state", "Choose a state:",
list(`East Coast` = c("NY"),
`West Coast` = c("WA", "OR", "CA"),
'North Coast' = c("canada"),
`Midwest` = c("MN", "WI", "IA"),
'south coast' = c("Argentina")
)
),
textOutput("result")
),
server = function(input, output) {
output$result <- renderText({
paste("You chose", input$state)
})
}
)
}

Altered list

任何人的想法如何规避这个?

最佳答案

如果对单个元素列表项使用命名向量,它将呈现组和子组。直播应用 here .

shinyApp(
ui = fluidPage(
selectInput("state", "Choose a state:",
list(`East Coast` = c("NY" = "ny"),
`West Coast` = c("WA", "OR", "CA"),
`North Coast` = c("Canada" ="canada"),
`Midwest` = c("MN", "WI", "IA"),
`south coast` = c("Argentina" = "argentina")
)
),
textOutput("result")
),
server = function(input, output) {
output$result <- renderText({
paste("You chose", input$state)
})
}
)

关于r - 使用单元素向量从命名列表中选择输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48402259/

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