How to use external columns with linked output in Shiny

微笑、不失礼 提交于 2020-01-16 08:21:07

问题


I have a very simple shiny app you can run below. It has linked inputs so when you change the "Department" input the "Displayname" is populated with people in that department. I need to go get some data from a database for the "DisplayName" selected. So if Department A & Frank is selected I need to go get Frank's data BUT I cannot pass the display name to the database. Luckily I can pass the "NameToPassToDatabase" but how can I access the corresponsing "NameToPassToDatabase"?

In the example below I instead of pass the name to the database I just pass it to RenderText and it prints on the screen.

For example

if Department A & Frank is selected I want to print "FG"

if Department B & Bill is selected I want to print "BU"

if Department C & TOM is selected I want to print "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:


You can simply index on both the department and name and just return the NameToPassToDatabase. Note, I added an additional name to Department C to demonstrate that you would return the correct code with respect to name and department.

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

    }
))


来源:https://stackoverflow.com/questions/28286674/how-to-use-external-columns-with-linked-output-in-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!