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

标签 r vector dynamic widget 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))

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

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

最佳答案

您可以像这样使用setNames:

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

关于R Shiny : Using a Vector for Display and another Vector for Value in Choice List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169612/

相关文章:

c - 使用 C qsort() 对结构体中的动态数组进行排序

r - 如何访问向量中的最后一个值?

c# - 我想要一个动态静态字段

r - 是什么导致了与时间序列属性相关的错误?

r - 使用 R 中的值计算计数按组执行频率表

c++ - 如何为 std::vector<std::vector<int>> 数据类型分配内存?

c++ - 如何将对象分配给与 operator= 不匹配的 2d vector

javascript - path.join 在 require 语句中

mysql - RMySQL - dbWriteTable() - 此 MySQL 版本不允许使用命令

r - pandoc:在新标签页中打开链接