如何在Shiny中使用带有链接输出的外部列

时间:2015-02-02 21:02:15

标签: r shiny

我有一个非常简单的闪亮应用程序,你可以在下面运行。它链接了输入,因此当您更改"部门"输入" Displayname"在该部门的人口中居住着。我需要从数据库中获取" DisplayName"选择。如果部门A&弗兰克被选中我需要去获取弗兰克的数据但是我无法将显示名称传递给数据库。幸运的是,我可以通过" NameToPassToDatabase"但我怎样才能访问相应的" NameToPassToDatabase"?

在下面的示例中,我不是将名称传递给数据库,而是将其传递给RenderText,然后在屏幕上打印。

例如

如果是A部门和A部门。弗兰克被选中我要打印" FG"

如果B部门和B部门Bill被选中我要打印" BU"

如果是C部门和选择TOM我要打印" TT"

require(shiny)

datas <- data.frame(Department = c("dept a", "dept b", "dept c"), DisplayName = c("Frank","Bill","Tom"), NameToPassToDatabase = c("FG","BU","TT"))

runApp(list(
  ui = basicPage(
    sidebarPanel(
      selectInput("Department", "Select a department", choices = levels(datas$Department), selected = levels(datas$Department)[1]),
      #selectInput("files", "Select files", choices = datas$file[datas$directory == levels(datas$directory)[1]], multiple = FALSE)
      uiOutput("DisplayName")
    ),
    mainPanel(textOutput("Text") )
  ),
  server = function(input, output, session) {


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


    output$Text <- renderText({
      print("in render text")
      return(input$DisplayName) #I actaully want to access the NameToPassToDatabase instead of the display name here
    })

  }
))

1 个答案:

答案 0 :(得分:0)

您可以简单地在部门和名称上编制索引,然后只返回NameToPassToDatabase。请注意,我在Department C中添加了一个额外的名称,以证明您将返回与名称和部门相关的正确代码。

datas <- data.frame(Department = c("dept a", "dept b", "dept c", "dept c"), 
                DisplayName = c("Frank","Bill","Tom","Frank"), 
                NameToPassToDatabase = c("FG","BU","TT","FC"))



runApp(list(
    ui = basicPage(
        sidebarPanel(
            selectInput("Department", "Select a department", 
                        choices = levels(datas$Department), 
                        selected = levels(datas$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(datas$DisplayName[datas$Department == Department]), 
                        selected = as.character(datas$DisplayName[datas$Department == Department][1]))
        })


        output$Text <- renderText({
            print("in render text")
            nameToDB <- as.character(
                datas[datas$DisplayName == input$DisplayName & 
                          datas$Department == input$Department,3])         
            return(nameToDB)         
        })

    }
))