【问题标题】:Multiple Text Inputs Shiny R多个文本输入 Shiny R
【发布时间】:2017-02-14 21:06:47
【问题描述】:

我一直试图弄清楚如何在 Shiny 中让多个输入与一个输出对象连接。

我有一个表格作为输出,我想根据用户在文本框中输入的内容仅显示该表格的特定行。在我的示例中,该表包含以下列:姓名、地址、出生日期、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)

【问题讨论】:

  • 如果 DateOfBirth 和 NextAppointment 不重叠,您可以添加规则来选择要过滤的字段

标签: r shiny


【解决方案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)

【讨论】:

  • 非常感谢您的回复。我看到你做了什么,但它似乎仍然不太有效。出于某种原因,当我只在“下一个约会”搜索框中输入内容时,搜索实际上并没有执行。没发生什么事。这可能是因为提交按钮只绑定到第一个输入框?我不确定。再次提前感谢您的帮助。
  • 没有更多的“下次约会”搜索框。在我的版本中,我不确定我是否理解问题:/
  • 你是对的!那是我的错。这确实有效,将所有内容组合到一个搜索框中。感谢您的帮助!
  • 你会不会知道我怎么能有两个文本输入?那将是理想的...我不想将太多东西分组到第一个搜索框中。再次感谢!
  • 我进行了编辑。还可以考虑 HubertL 的评论以进行进一步调整。
猜你喜欢
  • 1970-01-01
  • 2014-12-06
  • 2018-10-29
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 2019-08-21
相关资源
最近更新 更多