我(非常)是闪亮的新手,我正在尝试创建一个应用程序,用户可以在其中输入他们对一个或多个选项的选择,然后将他们的输入选择用于查询 SQL 数据库。例如,如果用户选择“Dublin”或“Aberdeen”,则应用程序使用RmySQL::dbReadTable()
从数据库中检索同名表并将其输出到表中。如果用户选择“Dublin”和“Aberdeen”,应用程序DBI::dbGetQuery()
会查询数据库以从每个表中选择某些列,然后将它们连接在一起,将结果输出到表中。
当只选择一个选项时,我可以让它工作,但不超过一个。
从这里Shiny 仅在 multiple = TRUE 时使用 selectInput 的第一项我假设我可以访问input$dataset
选择多个选项时的各个元素。
看过这里:Get value from reactive context in R shiny based on user input我尝试使用反应函数创建查询,然后在第二个反应函数中使用它,如下所示,但应用程序不会运行,我得到了这个错误:(函数(类,fdef,mtable)中的错误:无法找到签名'“MySQLConnection”,“reactiveExpr”'的函数'dbGetQuery'的继承方法</p>
library(shiny)
library(rsconnect)
library(RMySQL)
library(dplyr)
library(DBI)
ui = fluidPage(
titlePanel("EWAS database"),
sidebarLayout(
sidebarPanel(
selectInput("dataset", "Choose one or more datasets:",
choices = c("Dublin", "Aberdeen"), selected = NULL, multiple = TRUE),
numericInput("obs", "Number of results to view:", 10),
submitButton("Update View"),
downloadButton("downloadData", "Download")
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
conn <- dbConnect(drv = MySQL(), dbname = "EWAS", host = “xxx”,
username = “xxx”, password = “xxx”)
Dublin <- dbReadTable(conn = conn, name = 'Dublin', value = as.data.frame(Dublin))
Aberdeen <- dbReadTable(conn = conn, name = 'Aberdeen', value = as.data.frame(Aberdeen))
query <- reactive({paste0("'SELECT ",
input$dataset[1], ".", input$dataset[1], "_id, ",
input$dataset[1], ".", input$dataset[1], "_Status_Beta, " ,
input$dataset[1], ".", input$dataset[1], "_Status_SE, " ,
input$dataset[1], ".", input$dataset[1], "_Status_P, " ,
input$dataset[2], ".", input$dataset[2], "_id, " ,
input$dataset[2], ".", input$dataset[2], "_Status_Beta, " ,
input$dataset[2], ".", input$dataset[2], "_Status_SE, " ,
input$dataset[2], ".", input$dataset[2], "_Status_P, " ,
"From ", input$dataset[1], " LEFT JOIN ", input$dataset[2],
" ON ", input$dataset[1], ".", input$dataset[1], "_id ", "= ",
input$dataset[2], ".", input$dataset[2], "_id'")})
custom <- dbGetQuery(conn = conn, query)
datasetInput <- reactive({
if(identical(input$dataset, "Dublin")){
Dublin
} else if (identical(input$dataset, "Aberdeen")){
Aberdeen
} else
custom
})
output$table <- renderTable({
head(datasetInput(), n = input$obs)
})
output$downloadData <- downloadHandler(
filename = function(){
paste(input$dataset, ".csv", sep = "")
},
content = function(file){
write.csv(datasetInput(), file, row.names = F)
}
)
on.exit(dbDisconnect(conn), add = TRUE)
}
shinyApp(ui = ui, server = server)
我还尝试将查询直接放在反应函数中(如下所示),在这种情况下应用程序启动但我得到:RS_DBI_getConnection 中的内部错误:连接句柄损坏。
server <- function(input, output) {
conn <- dbConnect(drv = MySQL(), dbname = "EWAS", host = “xxx”,
username = “xxx”, password = “xxx”)
Dublin <- dbReadTable(conn = conn, name = 'Dublin', value = as.data.frame(Dublin))
Aberdeen <- dbReadTable(conn = conn, name = 'Aberdeen', value = as.data.frame(Aberdeen))
datasetInput <- reactive({
if(identical(input$dataset, "Dublin")){
Dublin
} else if (identical(input$dataset, "Aberdeen")){
Aberdeen
} else
dbGetQuery(conn, paste0("'SELECT ",
input$dataset[1], ".", input$dataset[1], "_id, ",
input$dataset[1], ".", input$dataset[1], "_Status_Beta, " ,
input$dataset[1], ".", input$dataset[1], "_Status_SE, " ,
input$dataset[1], ".", input$dataset[1], "_Status_P, " ,
input$dataset[2], ".", input$dataset[2], "_id, " ,
input$dataset[2], ".", input$dataset[2], "_Status_Beta, " ,
input$dataset[2], ".", input$dataset[2], "_Status_SE, " ,
input$dataset[2], ".", input$dataset[2], "_Status_P, " ,
"From ", input$dataset[1], " LEFT JOIN ", input$dataset[2],
" ON ", input$dataset[1], ".", input$dataset[1], "_id ", "= ",
input$dataset[2], ".", input$dataset[2], "_id'"))
})
还有下面的变化......根据这里的解决方案:如何使闪亮的反应性与SQL数据库一起工作?但是这次应用程序没有运行,我得到: dbGetQuery(conn = conn, query) 中的错误:找不到对象“查询”。
datasetInput <- reactive({
query <- paste0("'SELECT ",
input$dataset[1], ".", input$dataset[1], "_id, ",
input$dataset[1], ".", input$dataset[1], "_Status_Beta, " ,
input$dataset[1], ".", input$dataset[1], "_Status_SE, " ,
input$dataset[1], ".", input$dataset[1], "_Status_P, " ,
input$dataset[2], ".", input$dataset[2], "_id, " ,
input$dataset[2], ".", input$dataset[2], "_Status_Beta, " ,
input$dataset[2], ".", input$dataset[2], "_Status_SE, " ,
input$dataset[2], ".", input$dataset[2], "_Status_P, " ,
"From ", input$dataset[1], " LEFT JOIN ", input$dataset[2],
" ON ", input$dataset[1], ".", input$dataset[1], "_id ", "= ",
input$dataset[2], ".", input$dataset[2], "_id'")
if(identical(input$dataset, "Dublin")){
Dublin
} else if (identical(input$dataset, "Aberdeen")){
Aberdeen
} else
dbGetQuery(conn, query)
})
我还想我也许可以单独检索多个表,然后base::cbind()
只使用我想要的列将它们一起使用,但我无法弄清楚如何在这种情况下工作。
最后,我需要能够扩展应用程序以包含许多其他表,并且最终用户可以选择多达 3 个选项,其中包含以下内容:
if(length(input$dataset) == 1){
dbReadTable(conn, name = ‘input$dataset’, value = as.data.frame(input$dataset)
} else if (length(input$dataset) == 2){
dbGetQuery(conn, query2)
} else if (length(input$dataset) == 3){
dbGetQuery(conn, query3)
}
所以我真的希望任何解决方案都能考虑到这一点。任何帮助或建议将不胜感激!