你如何处理Shiny Dropdown列表中的ID?

时间:2015-02-03 17:05:18

标签: r shiny

通常在Web界面中,如果您从数据库中填充了一个下拉列表,该下拉列表显示了一些文本,并且您希望在下拉列表中使用该选定文本并将其传递回数据库。但很多时候你想传递一个ID而不是显示的实际文本。

在下面的示例中,我有一个global.R文件,它返回下拉列表的数据。这模拟了从数据库返回的数据。对于每个下拉列表,都会在下拉列表中显示一个文本字段,并显示一个" id"没有显示的字段但是我必须以某种方式访问​​" id"下拉菜单的字段。这是如何在Shiny中完成的?...因为selectInputs不允许你存储id所以你可以像输入$ DisplayName $ id那样访问它们

在下面的示例中,我只想打印" id" " DisplayName" selectInput如果" Mary"在输入$ DisplayName然后" 20"应该在RenderText调用中打印。

以下是要运行的代码:

require(shiny)

runApp(list(
  ui = basicPage(
    sidebarPanel(
      selectInput("Department", "Select a department", choices = as.character(GetDepartments()$Department), selected = as.character(GetDepartments()$Department[1])),
      uiOutput("DisplayName")
    ),
    mainPanel(textOutput("Text") )
  ),
  server = function(input, output, session) {


    output$DisplayName<-renderUI({
      Department <- input$Department
      print(Department)
      selectInput("DisplayName", 'DisplayName:', choices = as.character(GetDisplayName(Department)$DisplayName), selected =as.character(GetDisplayName(Department)$DisplayName[1] ))
    })


    output$Text <- renderText({

# Here I want to simulate accessing the "id" field of the input$DisplayName
#in my app I need to pass the id to a database query
#If Mary is in input$DisplayName how can I access her id of "20"?
      print("in render text") 

      return( ??? How do I access the id  = 20???) 
    })

  }
))

这是global.r文件,它模拟从数据库返回内容的代码

GetDepartments<- function(){
  df<- data.frame(Department= c("Dept A", "Dept B"), id = c(1,2))
  return(df)
}

GetDisplayName<- function(Dept){
  if(Dept == "Dept A")
  {
    df<- data.frame(DisplayName= c("Bob", "Fred"), id = c(4,6))
    return(df)
  }else
  {
    df<- data.frame(DisplayName= c("George", "Mary"), id = c(10,20))
    return(df)
  }

}

1 个答案:

答案 0 :(得分:0)

这与您的其他问题here非常相似。正如@nrussel所说,这只是一个简单的子集问题。只需提取您的部门和名称索引。这是一个有效的例子。

编辑*** - 使数据集具有反应性以避免重复。

根据documentation

反应式表达式比常规R函数更智能。它们缓存结果,只有在它们过时时才会更新。第一次运行反应式表达式时,表达式会将其结果保存在计算机的内存中。下次调用反应式表达式时,它可以返回此保存的结果而不进行任何计算(这将使您的应用程序更快)。反应表达式将使用此新副本,直到它过时。

runApp(list(
    ui = basicPage(
        sidebarPanel(
            selectInput("Department", "Select a department", 
                        choices = as.character(GetDepartments()$Department), 
                        selected = as.character(GetDepartments()$Department[1])),
            uiOutput("DisplayName")
        ),
        mainPanel(textOutput("Text") )
    ),
    server = function(input, output, session) {

        myData <- reactive({
            GetDisplayName(input$Department)
        })

        output$DisplayName<-renderUI({
            Department <- input$Department
            print(Department)
            myData <- myData()
            selectInput("DisplayName", 'DisplayName:', choices = as.character(myData$DisplayName), 
                        selected =as.character(myData$DisplayName[1] ))
        })


        output$Text <- renderText({
            print("in render text") 
            myData <- myData()
            code <- as.character(myData[myData$DisplayName == input$DisplayName,2])
            return(code)       
        })

    }
))