如何在RShiny应用程序中降低SQL查询的复杂性和长度?

如何在RShiny应用程序中降低SQL查询的复杂性和长度?,sql,shiny,shiny-server,shinyapps,Sql,Shiny,Shiny Server,Shinyapps,我正在使用RShiny构建一个应用程序,它可以查询SQLite数据库。 我已经完成了应用程序的代码编写,但在降低查询/使用glue_sql函数的复杂性方面遇到了困难。就目前而言,我认为有许多OR语句可能会减慢查询速度。 我使用sqlInterpolate来执行查询以防止任何SQL注入,因为这是RShiny网站上引用的应该做的事情 这是我目前的代码: 用户界面: ui您的查询需要一个GROUP BY子句。您是对的,谢谢您的提醒,这是我在RStudio中的代码中的错误。您可以在WARE语句中将ORs

我正在使用RShiny构建一个应用程序,它可以查询SQLite数据库。 我已经完成了应用程序的代码编写,但在降低查询/使用glue_sql函数的复杂性方面遇到了困难。就目前而言,我认为有许多OR语句可能会减慢查询速度。 我使用sqlInterpolate来执行查询以防止任何SQL注入,因为这是RShiny网站上引用的应该做的事情

这是我目前的代码:

用户界面:


ui您的查询需要一个GROUP BY子句。您是对的,谢谢您的提醒,这是我在RStudio中的代码中的错误。您可以在WARE语句中将ORs替换为
in
WHERE study in(?studytype1,?studytype2,…)
ui <- fluidPage(theme = shinytheme("cerulean"),
titlePanel("Application"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput(inputId = "Assay", label = "Assay Type:", choices = c("MBP1","EV","Oxidation","WGAS","WLS","Genomics"),
selected = c("MBP1","EV","Oxidation","WGAS","WLS","Genomics")), #Assay
checkboxGroupInput(inputId = "Vendor", label = "Vendor:", choices = c("AKE","BSA"),
selected = c("AKE","BSA")), #Vendor
actionButton("read", "Read From Database"),
checkboxGroupInput(inputId = "Study",
                    label = "Study Type",
                    choices = c("MP", "CSP"),
selected = c("MP", "CSP")),
dateRangeInput(inputId = "Date", label = "Sample Date Range:", format ="yyyy-mm-dd"), #Date
actionButton("read", "Read From Database")),
        mainPanel(h1("Sample Count:"),
 dataTableOutput("Samples_Sorted_by_Study"),
 dataTableOutput("Samples_Sorted_by_Assay_and_Vendor")) #table
    ) #closing navbarPage
) #closing fluidPage UI
server <- function(input, output){
    #Storing values in myData variable
    myData <- reactiveValues()
    observeEvent(
        input$read,
        {
            myData$assay <- input$Assay
            myData$vendor <- input$Vendor
            myData$date_val <- input$Date
            myData$studytype <- input$Study

            #Opening database connection
            connectiontodb <- dbConnect(RSQLite::SQLite(), "example.sqlite")

            #Shortening Query??
            #myData$study_int <- c(myData$studytype[1], myData$studytype[2])
            #myData$study_int <- glue_sql("{myData$study_int*}")
            
            #conducting first query of samples grouped by study 
            #Sample query of example.sqlite database

            myData$interpolatequery1 <- sqlInterpolate(connectiontodb,
            "Select study, count(genomic_id), count(specimen_id)
            FROM exampletable WHERE study = ?studytype1 OR study = ?studytype2
            GROUP BY study", 
            studytype1 = myData$studytype[1], studytype2 = myData$studytype[2])

            myData$exampledataSQLQuery1 <- dbGetQuery(connectiontodb, myData$interpolatequery1)

            #Closing connection to database
            dbDisconnect(connectiontodb)
  
            #Correcting Column Names
            colnames(myData$exampledataSQLQuery1) <- c("Study", "Genomic Id", "Specimen Id")     
 }
    )
    
    output$Samples_Sorted_by_Study <- renderDataTable(myData$exampleSQLQuery1)
    

}

shinyApp(ui = ui, server = server)