Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 制作闪亮列表下拉列表选择输入_R_List_Shiny - Fatal编程技术网

R 制作闪亮列表下拉列表选择输入

R 制作闪亮列表下拉列表选择输入,r,list,shiny,R,List,Shiny,闪亮的selectInput小部件需要以下格式的命名选项列表: choices = list( "mpg" = 1, "cyl" = 2, "disp" = 3, "hp" = 4 # ..... etc ) 进入我的闪亮应用程序的数据帧将不会有相同的变量名,因此我想动态生成名称列表 以下是一个尝试: data(mtcars) choices = data.frame( var = names(mtcars), num = 1:length(names(mtcar

闪亮的selectInput小部件需要以下格式的命名选项列表:

choices = list(
  "mpg" = 1,
  "cyl" = 2,
  "disp" = 3,
  "hp" = 4
  # ..... etc
)
进入我的闪亮应用程序的数据帧将不会有相同的变量名,因此我想动态生成名称列表

以下是一个尝试:

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 = ",")
          )
    )
...


我如何才能做到这一点?

嗨,如果我正确理解了你的问题,你可以这样做:

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)
数据(mtcars)
选项=data.frame(
var=名称(mtcars),
num=1:长度(名称(mtcars))
)
#selectInput的选项列表

我的维克托,你能看看这两个问题吗?非常感谢你!