在R shiny中的selectInput小部件中获取名称

时间:2017-06-27 15:13:56

标签: html r shiny

我正在使用闪亮包的selectInput函数和选项组,例如Output of the selectInput function

在ui.r文件中我有类似的东西:

ListOfItemsWithNames = list(condition = c("KO","WT"),treatment = c("non","oui"))    
selectInput("Select1_contrast",label="Compare",ListOfItemsWithNames)

在server.R文件中,当我调用input $ Select1_contrast时,我只得到所选的值(例如“oui”)。

有没有办法让价值成为变量的名称(即“oui”和“treatment”)?

2 个答案:

答案 0 :(得分:0)

这应该有效。在这个版本中,你有一个sencond下拉菜单,因此有第二个输入。

library(shiny)

ListOfItemsWithNames = list(condition = c("KO","WT"),treatment = c("non","oui"))  

ui = inputPanel(
  selectInput("category", "choose a category", names(ListOfItemsWithNames )),
  selectInput("choice", "select a choice", ListOfItemsWithNames[[1]])
)

server = function(input, output, session){
  observe({
    updateSelectInput(session, "choice", 
      choices = ListOfItemsWithNames[[input$category]])
  })
}

shinyApp(ui, server)

答案 1 :(得分:0)

这是另一种可能性。它使用键值对。根据{{​​1}}

的文档允许这些对
  

选项可供选择的值列表。如果列出了列表的元素,则会向用户显示该名称而不是值。这也可以是命名列表,其元素是(命名或未命名的)列表或向量。如果是这种情况,最外面的名称将用作" optgroup"相应子列表中元素的标签。这允许您对类似的选项进行分组和标记。有关此功能的小型演示,请参阅示例部分。

selectInput

enter image description here

如您所见,addKeys = function(nested_list){ keyed_nl = list() for (a in names(nested_list)) for (b in (nested_list[[a]])) keyed_nl[[a]][[b]] = paste0(a, "-", b) keyed_nl } ListOfItemsWithNames = list(condition = c("KO", "WT"), treatment = c("non", "oui")) keyedList = addKeys(ListOfItemsWithNames) library(shiny) shinyApp( fluidPage( selectInput("choiceKey", "choose", keyedList), textOutput('text') ), function(input, output, session) output$text = renderText(input$choiceKey) ) 将为您提供与input$choiceKey分开的类别和选择。使用-,您可以单独获取这两个部分