R Shiny:使用矢量显示和另一个矢量选择列表中的值

SEH*_*ETT 6 r vector widget dynamic shiny

我很好奇是否有一种方法可以使用向量作为显示值,另一个向量作为选择列表中的实际值或名称,而无需显式声明每个值.

这是一个简单的例子:

 # vect1 is the vector I would like as the display list
 vect1 <- c("A", "B", "C", "D", "E")

 # vect2 is the vector I would like as the name group

 vect2 <- c("01", "02", "03", "04", "05")


  # To get the desired output, this is how I would create a selectInput widget on the 
  # ui side to get the desired outcome.
  selectInput("exampleinput", 
              choices = list("A" = "01", 
                             "B" = "02", 
                             "C" = "03", 
                             "D" = "04", 
                             "E" = "05"))


 # Instead, I would like to do something like the following to create the same output:
 selectInput("exampleinput", choices = list(vect1 = vect2))
Run Code Online (Sandbox Code Playgroud)

我不确定这是否可能,但它会非常方便和干净.应用程序是我有不同状态的代码,对应用程序的用户没有意义.对我来说,代码是加载数据所必需的.我实际使用的向量是动态创建的.我的应用程序工作得很好,我知道我可以轻松编写一个函数来将我希望显示的状态缩写转换为我将在幕后使用的状态代码; 如果像上面这样的东西是可能的话,它会更加清洁和容易.

提前感谢您的所有帮助!

tca*_*h21 6

你可以setNames像这样使用:

choices = setNames(vect2,vect1)
selectInput("exampleinput","exampleinput", choices = choices)
Run Code Online (Sandbox Code Playgroud)

  • 请更新您的答案:`choices = setNames(vect2,vect1)selectInput("exampleinput","exampleinput",choices = choices)`因为每个请求的输出应该是反向的,而`selectInput`需要一个`label` (2认同)