gpt4 book ai didi

r - 列出 Shiny 的下拉列表selectInput

转载 作者:行者123 更新时间:2023-12-04 08:05:50 25 4
gpt4 key购买 nike

Shiny 的selectInput小部件需要以下格式的命名选择列表:

choices = list(
"mpg" = 1,
"cyl" = 2,
"disp" = 3,
"hp" = 4
# ..... etc
)

转到我 Shiny 的应用程序的数据框将不会具有相同的变量名称,因此我想即时生成名称列表。

这是一个尝试:
data(mtcars)

choices = data.frame(
var = names(mtcars),
num = 1:length(names(mtcars))
)

> head(choices)
var num mylist
1 mpg 1 "mpg" = 1
2 cyl 2 "cyl" = 2
3 disp 3 "disp" = 3
4 hp 4 "hp" = 4
5 drat 5 "drat" = 5
6 wt 6 "wt" = 6

paste(choices$mylist, collapse = ",")

这看起来很近,但是不起作用:
...
box(
selectInput(
"select",
label = h3("Select box"),
choices = list(
paste(choices$mylist, collapse = ",")
)
)
...

enter image description here

我该如何进行这项工作?

最佳答案

嗨,如果我理解您的问题对,您可以这样做:

data(mtcars)

choices = data.frame(
var = names(mtcars),
num = 1:length(names(mtcars))
)
# List of choices for selectInput
mylist <- as.list(choices$num)
# Name it
names(mylist) <- choices$var
# ui
ui <- fluidPage(
selectInput(
"select",
label = h3("Select box"),
choices = mylist
)
)
# server
server <- function(input, output) {

}
# app
shinyApp(ui = ui, server = server)

关于r - 列出 Shiny 的下拉列表selectInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105044/

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