R 多文本输入
我一直在试图弄清楚如何让多个输入连接到Shiny中的一个输出对象 我有一个表作为输出,我只想根据用户在文本框中输入的内容显示该表的特定行。在我的示例中,该表有以下列:Name、Address、DateOfBirth、ID、Next Appointment 我根据正则表达式过滤用户的输入,这很有效,但在尝试区分DateOfBirth和NextAppoint时,它会崩溃,因为它们都是YYYY-MM-DD 如何创建一个不会干扰第一个文本输入的新文本输入?我不能让它工作。我是否需要使用提交按钮以外的其他按钮 我的程序现在只会根据第一个文本输入框进行搜索。第二个文本输入框未激活。这就是我需要你帮助的地方 非常感谢。以下是我的示例应用程序代码:R 多文本输入,r,shiny,R,Shiny,我一直在试图弄清楚如何让多个输入连接到Shiny中的一个输出对象 我有一个表作为输出,我只想根据用户在文本框中输入的内容显示该表的特定行。在我的示例中,该表有以下列:Name、Address、DateOfBirth、ID、Next Appointment 我根据正则表达式过滤用户的输入,这很有效,但在尝试区分DateOfBirth和NextAppoint时,它会崩溃,因为它们都是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)
库(闪亮)
#用户界面
#--------------------------------------------------------------------
ui=新友(带侧栏的页面)(
headerPanel(“测试应用程序”),
侧栏面板(
#声明2个文本输入并提交按钮
textInput(inputId=“variableInput”,label=“按姓名、ID或出生日期搜索”),
textInput(inputId=“nextapt”,“按下一次约会搜索”),
提交按钮(text=“提交”)
),
主面板(
#声明文本输出
#我不想有两个表输出
#理想情况下,我会有2个搜索框,用于1个表
表输出(“变量”)
#,表格输出(“下一步”)
)
))
#服务器.R
#--------------------------------------------------------------------
服务器=shinyServer(功能(输入、输出){
#使用值创建示例表。每个向量表示一列
姓名=c(“人1”、“人2”、“人3”)
地址=c(“101东街”、“102东街”、“103东街”)
出生日期=c(“1990-01-01”、“1990-01-02”、“1990-01-03”)
ID=c(“12345”、“23456”、“34567”)
NextAppoint=c(“2017-02-14”、“2017-02-15”、“2017-02-16”)
df=data.frame(姓名、地址、出生日期、ID、下一个地址)
#使用正则表达式确定用户正在搜索的内容
#如果用户输入像#####-#-#-#,其中#是任意数字,
#那么他们一定输入了日期
#如果用户输入######,则它必须是一个ID
#否则,他们会输入一个名字
#search.criteria()是要显示的数据帧行的向量
search.criteria这对你有用吗
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)
库(闪亮)
#用户界面
#--------------------------------------------------------------------
ui=新友(带侧栏的页面)(
headerPanel(“测试应用程序”),
侧栏面板(
#声明2个文本输入并提交按钮
textInput(inputId=“variableInput”,label=“按姓名、ID、出生日期搜索”),
textInput(inputId=“nextapt”,label=“下次约会”),
提交按钮(text=“提交”)
),
主面板(
#声明文本输出
#我不想有两个表输出
#理想情况下,我会有2个搜索框,用于1个表
表输出(“变量”)
#,表格输出(“下一步”)
)
))
#服务器.R
#--------------------------------------------------------------------
服务器=shinyServer(功能(输入、输出){
#使用值创建示例表。每个向量表示一列
姓名=c(“人1”、“人2”、“人3”)
地址=c(“101东街”、“102东街”、“103东街”)
出生日期=c(“1990-01-01”、“1990-01-02”、“1990-01-03”)
ID=c(“12345”、“23456”、“34567”)
NextAppoint=c(“2017-02-14”、“2017-02-15”、“2017-02-16”)
df=data.frame(姓名、地址、出生日期、ID、下一个地址)
#使用正则表达式确定用户正在搜索的内容
#如果用户输入像#####-#-#-#,其中#是任意数字,
#那么他们一定输入了日期
#如果用户输入######,则它必须是一个ID
#否则,他们会输入一个名字
#search.criteria()是要显示的数据帧行的向量
search.criteria如果DateOfBirth和NextAppoint不重叠,您可以添加一个规则来选择要筛选的字段非常感谢您的答复。我看到了您所做的,但似乎仍然不太有效。由于某些原因,当我只在“下一个约会”中键入内容时,搜索实际上不会执行搜索框。什么也没有发生。这可能是因为提交按钮仅绑定到第一个输入框?我不确定。再次提前感谢您的帮助。不再有“下一次约会”搜索框。在我的版本中,我不确定我是否理解这个问题:/你是对的!这是我的错。这确实有效,将所有内容合并到一个搜索框中。谢谢你的帮助!你是否知道我如何可以将其设为两个文本输入?这将是理想的……我不想在第一个搜索框中组合太多内容。再次感谢!我做了一个编辑,也考虑了Hubtl的注释,以进行进一步的修改。