2017-02-14 206 views
1

我一直在試圖弄清楚如何讓多個輸入與Shiny中的一個輸出對象連接。多個文本輸入閃亮R

我有一個表作爲輸出,我只想顯示該表的特定行,具體取決於用戶在文本框中輸入的內容。在我的示例中,該表包含以下列:名稱,地址,DateOfBirth,ID,下次約會。

我過濾了基於正則表達式的用戶輸入,這很好用,但是當它試圖區分DateOfBirth和NextAppointment時,由於它們都是YYYY-MM-DD,所以它會崩潰。

如何創建不會干擾第一個文本輸入的新文本輸入?我無法得到這個工作。我需要使用提交按鈕以外的東西嗎?

我的程序現在只會根據第一個文本輸入框進行搜索。第二個文本輸入框不活動。這是我需要你幫忙的地方。

非常感謝。這裏是我的示例應用程序代碼:

library(shiny) 

#ui.R 
#-------------------------------------------------------------------- 
ui = shinyUI(pageWithSidebar(
    headerPanel("Test App"), 
    sidebarPanel(
    #declare 2 text inputs and submit button 
    textInput(inputId = "variableInput", label = "Search by Name, ID or Date of Birth"), 
    textInput(inputId = "NextAppt", "Search by Next Appointment"), 
    submitButton(text = "Submit") 
), 
    mainPanel(
    #declare text output(s) 
    #I don't want to have 2 table outputs 
    #ideally I would have 2 search boxes for 1 table 
    tableOutput("Variable") 
    #,tableOutput("NextAppt") 
) 
)) 


#server.R 
#-------------------------------------------------------------------- 
server = shinyServer(function(input, output){ 
    #make sample table with values. each vector represents a column 
    Name = c("Person1", "Person2", "Person3") 
    Address = c("101 E St", "102 E St", "103 E St") 
    DateOfBirth = c("1990-01-01", "1990-01-02", "1990-01-03") 
    ID = c("12345", "23456", "34567") 
    NextAppointment = c("2017-02-14", "2017-02-15", "2017-02-16") 
    df = data.frame(Name, Address, DateOfBirth, ID, NextAppointment) 

    #determine what the user is searching for by using regular expressions 
    #if the user entered something like ####-##-##, where # is any number, 
    #then they must have entered a date 
    #if the user enters #####, then it must be an ID 
    #otherwise, they entered a name 
    #search.criteria() is a vector of the rows of our dataframe to display 
    search.criteria <- reactive({ 
    if(grepl("\\d{4}\\-\\d{2}\\-\\d{2}", input$variableInput)==TRUE){ 
     which(df$DateOfBirth==input$variableInput) 
    } else if(grepl("\\d{5}", input$variableInput)==TRUE){ 
     which(df$ID==input$variableInput) 
    } else{ 
     which(df$Name==input$variableInput) 
    } 
    }) 

    #create output table 
    output$Variable = renderTable({ 
    df[search.criteria(), ] #use the search.critera() reactive to determine rows to display 
    }) 

}) 


#app.R 
#-------------------------------------------------------------------- 
shinyApp(ui, server) 
+0

如果DateOfBirth和NextAppointment不重疊,則可以添加規則以選擇要過濾的字段 – HubertL

回答

1

這會適合你嗎?

library(shiny) 

#ui.R 
#-------------------------------------------------------------------- 
ui = shinyUI(pageWithSidebar(
    headerPanel("Test App"), 
    sidebarPanel(
    #declare 2 text inputs and submit button 
    textInput(inputId = "variableInput", label = "Search by Name, ID, Date of Birth"), 
    textInput(inputId = "NextAppt", label = "Next Appointment"), 
    submitButton(text = "Submit") 
), 
    mainPanel(
    #declare text output(s) 
    #I don't want to have 2 table outputs 
    #ideally I would have 2 search boxes for 1 table 
    tableOutput("Variable") 
    #,tableOutput("NextAppt") 
) 
)) 


#server.R 
#-------------------------------------------------------------------- 
server = shinyServer(function(input, output){ 
    #make sample table with values. each vector represents a column 
    Name = c("Person1", "Person2", "Person3") 
    Address = c("101 E St", "102 E St", "103 E St") 
    DateOfBirth = c("1990-01-01", "1990-01-02", "1990-01-03") 
    ID = c("12345", "23456", "34567") 
    NextAppointment = c("2017-02-14", "2017-02-15", "2017-02-16") 
    df = data.frame(Name, Address, DateOfBirth, ID, NextAppointment) 

    #determine what the user is searching for by using regular expressions 
    #if the user entered something like ####-##-##, where # is any number, 
    #then they must have entered a date 
    #if the user enters #####, then it must be an ID 
    #otherwise, they entered a name 
    #search.criteria() is a vector of the rows of our dataframe to display 
    search.criteria <- reactive({ 
    out <- c() 
    outAppt <- c() 
    if(grepl("\\d{4}\\-\\d{2}\\-\\d{2}", input$variableInput)==TRUE){ 
     out <- which(df$DateOfBirth==input$variableInput) 
     print(out) 
    } else if(grepl("\\d{5}", input$variableInput)==TRUE){ 
     out <- which(df$ID==input$variableInput) 
    } else{ 
     out <- which(df$Name==input$variableInput) 
    } 
    # filter for appointment 
    if(grepl("\\d{4}\\-\\d{2}\\-\\d{2}", input$NextAppt)==TRUE){ 
     outAppt <- which(df$NextAppointment==input$NextAppt) 
     if(length(out)){ 
     out <- intersect(out, outAppt) 
     }else{ 
     out <- outAppt 
     } 
    } 
    out 
    }) 

    #create output table 
    output$Variable = renderTable({ 
    print(search.criteria()) 
    df[search.criteria(), ] #use the search.critera() reactive to determine rows to display 
    }) 

}) 


#app.R 
#-------------------------------------------------------------------- 
shinyApp(ui, server) 
+0

非常感謝您的回覆。我看到你做了什麼,但它似乎仍然不工作。出於某種原因,當我只在「Next Appointment」搜索框中輸入內容時,搜索實際上不會執行。什麼都沒發生。這可能是因爲提交按鈕只綁定到第一個輸入框?我不確定。再次感謝您的幫助。 – tsouchlarakis

+0

沒有更多「Next Appointment」搜索框。在我的版本中,我不確定我是否理解這個問題:/ – BigDataScientist

+0

你是對的!那是我的錯。這確實有效,將所有內容組合到一個搜索框中。感謝您的幫助! – tsouchlarakis