简体   繁体   English

多个文本输入闪亮R

[英]Multiple Text Inputs Shiny R

I have been trying to figure out how to have multiple inputs connect with one output object in Shiny. 我一直在尝试弄清楚在Shiny中如何使多个输入与一个输出对象连接。

I have a table as an output, and I want to show only specific rows of that table depending on what the user enters in a text box. 我有一个表作为输出,并且我只想显示该表的特定行,具体取决于用户在文本框中输入的内容。 In my example, the table has columns: Name, Address, DateOfBirth, ID, Next Appointment. 在我的示例中,该表具有以下列:名称,地址,出生日期,ID,下次约会。

I filter the user's input based on regular expressions, which works great, but it breaks down when trying to differentiate between DateOfBirth and NextAppointment, since they are both YYYY-MM-DD. 我根据正则表达式过滤了用户的输入,效果很好,但是在尝试区分DateOfBirth和NextAppointment时会失败,因为它们都是YYYY-MM-DD。

How can I create a new text input that will not interfere with the first? 如何创建不会干扰第一个文本的新文本输入? I cannot get this to work. 我无法使它正常工作。 Do I need to use something other than the submit button? 除了提交按钮,我是否需要使用其他东西?

My program right now will only search based on the first text input box. 现在,我的程序将仅基于第一个文本输入框进行搜索。 The second text input box is not active. 第二个文本输入框未激活。 This is where I need your help. 这是我需要您帮助的地方。

Thanks very much in advance. 首先十分感谢。 Here is my sample app code: 这是我的示例应用代码:

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)

Would that work for you? 那对你有用吗?

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM