R Shiny:在选择列表中使用一个向量进行显示,另一个向量用于值

R Shiny: Using a Vector for Display and another Vector for Value in Choice List

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

这是一个简单的例子:

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

我不确定这是否可行,但它会非常方便和干净。该应用程序是我有不同状态的代码,这些代码对应用程序的用户来说毫无意义。对我来说,代码是加载数据所必需的。我实际使用的向量是动态创建的。我的应用程序运行良好,我知道我可以轻松地编写一个函数来将我希望显示的州缩写转换为我将在幕后使用的州代码;如果像上面这样的事情是可能的,那就更干净、更容易了。

预先感谢您提供的所有帮助!

您可以这样使用 setNames

choices = setNames(vect2,vect1)
selectInput("exampleinput","exampleinput", choices = choices)