我有一个应用程序,允许用户使用闪亮的应用程序上的输入小部件来查询MySQL数据库。查询也涉及联接表。使用IF ... ELSE语句来确定小部件是否为空或是否具有某些用户输入(如下面的代码)时,代码变得太长。
可以如下创建示例MySQL数据:
CREATE TABLE quoteauthors (
FirstName VARCHAR(255) ,
LastName VARCHAR(255) ,
authorID VARCHAR(255)
);
CREATE TABLE quotes (
quote VARCHAR(255) ,
authorID VARCHAR(255)
);
INSERT INTO quoteauthors
VALUES ('Albert', 'Einstein', 'a1'),
('Stephen', 'Hawking', 'a2'),
('Isaac', 'Newton', 'a3');
INSERT INTO quotes
VALUES ('Unthinking respect for authority is the greatest enemy of truth.', 'a1'),
('In the middle of difficulty lies opportunity.', 'a1'),
('Intelligence is the ability to adapt to change.', 'a2'),
('Science is not only a disciple of reason but, also, one of romance and passion.', 'a2'),
('If I have seen further it is by standing on the shoulders of Giants.', 'a3'),
('I can calculate the motion of heavenly bodies but not the madness of people', 'a3');
示例闪亮的应用程序如下:
library(shiny)
library(shinydashboard)
library(DBI)
library(RMySQL)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("QUOTE Search", tabName = "Tabs", icon = icon("object-ungroup"))
)
),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="Search ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
textInput("quoteSearch1", " Search Term 1 ", '', placeholder = "Type search term"),
radioButtons("combi", "Logical Operator to Combine Terms:",
c(
"AND" = "AND",
"OR" = "OR"
), inline = TRUE),
textInput("quoteSearch2", " Search Term 2 ", '', placeholder = "Type search term"),
selectInput("authorchoice", "Select AUTHOR", selected = NULL, multiple = T,
choices=c('Albert','Stephen','Isaac')),
submitButton("Search")
)
),
column( width=9,
tabBox(
width="100%",
tabPanel("Search Results",
htmlOutput("quotesearchdetails")
)))))))
server <- function(input, output) {
output$quotesearchdetails <-renderUI({
if(input$quoteSearch1!=""){
con <- dbConnect(MySQL(),
user='XXXXXXXXXXX',
port = 3306, password='XXXXXXXXXXX',
dbname='XXXXXXXXXXX',
host='XXXXXXXXXXX')
dbSendQuery(con, "SET NAMES utf8mb4;")
dbSendQuery(con, "SET CHARACTER SET utf8mb4;")
dbSendQuery(con, "SET character_set_connection=utf8mb4;")
on.exit(dbDisconnect(con), add = TRUE)
quotedetails <- reactive({
if (input$authorchoice == ""){
if (input$quoteSearch2 == ""){
dbGetQuery(con, statement =
paste0(" SELECT q.quote, a.FirstName, a.LastName
FROM quotes q
JOIN quoteauthors a
ON (q.authorID = a.authorID)
WHERE (q.quote LIKE '%",input$quoteSearch1,"%') "))
}else{
if (input$combi == "AND"){
dbGetQuery(con, statement =
paste0("
SELECT q.quote, a.FirstName, a.LastName
FROM quotes q
JOIN quoteauthors a
ON (q.authorID = a.authorID)
WHERE (q.quote LIKE '%",input$quoteSearch1,"%' AND
q.quote LIKE '%",input$quoteSearch2,"%')"))
}else{
dbGetQuery(con, statement =
paste0("
SELECT q.quote, a.FirstName, a.LastName
FROM quotes q
JOIN quoteauthors a
ON (q.authorID = a.authorID)
WHERE (q.quote LIKE '%",input$quoteSearch1,"%'
OR q.quote LIKE '%",input$quoteSearch2,"%')"))
}
}
}else{
if (input$quoteSearch2 == ""){
dbGetQuery(con, statement =
paste0("
SELECT q.quote, a.FirstName, a.LastName
FROM quotes q
JOIN quoteauthors a
ON (q.authorID = a.authorID)
WHERE (q.quote LIKE
'%",input$quoteSearch1,"%'
AND a.FirstName LIKE '%",input$authorchoice,"%') "))
}else {
if (input$combi == "AND"){
dbGetQuery(con, statement =
paste0("
SELECT q.quote, a.FirstName, a.LastName
FROM quotes q
JOIN quoteauthors a
ON (q.authorID = a.authorID)
WHERE (q.quote LIKE '%",input$quoteSearch1,"%' AND
q.quote LIKE '%",input$quoteSearch2,"%') AND
a.FirstName LIKE '%",input$authorchoice,"%' "))
}else{
dbGetQuery(con, statement =
paste0("
SELECT q.quote, a.FirstName, a.LastName
FROM quotes q
JOIN quoteauthors a
ON (q.authorID = a.authorID)
WHERE (q.quote LIKE '%",input$quoteSearch1,"%' OR
q.quote LIKE '%",input$quoteSearch2,"%')
AND
a.FirstName LIKE '%",input$authorchoice,"%' "))
}
}
}
})
outputed=""
quotedetailsreturned <- quotedetails()
if (dim(quotedetailsreturned)[1] > 0){
for(i in seq(from=1,to=dim(quotedetailsreturned)[1])){
outputed<-paste(outputed,
paste("Author's First name: ",quotedetailsreturned[i,"FirstName"]),
sep="<br/><br/>")
outputed<-paste(outputed,
paste("Author's Last name: ",quotedetailsreturned[i,"LastName"]),
sep="<br/><br/>")
outputed<-paste(outputed,
paste("Quote: ",quotedetailsreturned[i,"quote"]),
sep="<br/><br/>")
}
} else { outputed <-"your search yielded no results."}
HTML(outputed)
}else {
paste("Please input a search term at least in the first field")
}
})
}
shinyApp(ui, server)
我正在寻找一种解决方案,该如何使用代码中的IF ... ELSE语句来避免重复代码和长代码。我可以使用哪些最佳编程做法,将MySQL查询与用户在各种 textInput , radioButtons , select //选择输入,以此类推,考虑到某些输入可以保留为空,因此不应在查询中考虑。
答案 0 :(得分:0)
我首先将仅逐步构建查询字符串,然后根据所选设置逐步添加每个子句。构建完成后,执行查询。使代码更短,更易于阅读。