2017-02-14 18 views
1

Shinyの1つの出力オブジェクトに複数の入力を接続する方法を理解しようとしています。複数のテキスト入力Shiny R

私は出力としてテーブルを持っており、ユーザーがテキストボックスに入力した内容に応じて、そのテーブルの特定の行のみを表示します。私の例では、テーブルには名前、住所、DateOfBirth、ID、Next Appointmentという列があります。

正規表現に基づいてユーザーの入力をフィルタリングしますが、これはうまく機能しますが、DateOfBirthとNextAppointmentを区別しようとすると、YYYY-MM-DDであるために分割されます。

最初のテキスト入力に干渉しない新しいテキスト入力を作成するにはどうすればよいですか?私はこれを働かせることはできません。送信ボタン以外のものを使用する必要がありますか?

私のプログラムは、最初のテキスト入力ボックスのみに基づいて検索します。 2番目のテキスト入力ボックスはアクティブではありません。これが私の助けが必要なところです。

ありがとうございます。ここに私のサンプルアプリケーションコードがあります:

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

をフィルタリングするフィールドを選択するようにルールを追加することができますが、あなたの応答をありがとうございました。私はあなたが何をしたかを見ていますが、それでもまだそれはうまくいかないようです。何らかの理由で、「次の予定」の検索ボックスに何かを入力しただけでは、検索は実際には実行されません。何も起こりません。これは、送信ボタンが最初の入力ボックスにのみ関連付けられているためですか?私はわかりません。あなたの助けを前もっておかげです。 – tsouchlarakis

+0

"次の予定"の検索ボックスはありません。私のバージョンでは、私は問題を理解しているか分からない:/ – BigDataScientist

+0

あなたは正しい!それは私のせいだった。これは機能し、すべてを1つの検索ボックスにまとめます。ご協力ありがとうございました! – tsouchlarakis

関連する問題