我想构建一个连接到数据库的Shiny应用程序,我的问题是如何动态创建查询,这里我举一个例子,用户有四个研究轴(你只需要复制你的R中的代码)环境),但在我的应用程序中,我有12个研究轴,我不知道如何管理查询,如果你有一个想法?
# rm(list=ls())
library(sqldf)
library(car)
data(Moore)
ContextElements = colnames(Moore)
ContextElements[1] = 'partner_status'
library(shiny)
runApp(list(
ui = shinyUI(fluidPage(
titlePanel(h3("Data Select", style="background-color:gray")),
hr(),
div(class="row-fluid", style="height=350px; background-color:lightgray",
column(width=3,
uiOutput("firstAxe"),
uiOutput("firstAxeOutput")
),
column(width=3,
uiOutput("secondAxe"),
uiOutput('type1'),
uiOutput("secondAxeOutput")
),
column(width=3,
uiOutput("thirdAxe"),
uiOutput('type2'),
uiOutput("thirdAxeOutput")
),
column(width=3,
uiOutput("fourthAxe"),
uiOutput('type3'),
uiOutput("fourthAxeOutput")
)
),
hr(),
hr(),
div(class="row-fluid",
column(12,
verbatimTextOutput("mainQuery"))),
hr(),
div(class="row-fluid",
column(12,
verbatimTextOutput("query1"))),
div(class="row-fluid",
column(12,
verbatimTextOutput("query2"))),
div(class="row-fluid",
column(12,
verbatimTextOutput("query3")))
)),
server=function(input, output){
buildQuery= reactive({
})
output$mainQuery = renderPrint(buildQuery())
###############
### first Axis
output$firstAxe= renderUI({
selectInput(inputId = "firstAxe", label = h4("First Axis"),
choices=c(Choose='',ContextElements), multiple = FALSE)
})
# output Query
output$firstAxeOutput=renderUI({
if(!is.null(input$firstAxe)){
if(input$firstAxe==""){
selectInput(inputId = "firstAxeOutput", label="", choices = NULL, multiple = TRUE, selectize = FALSE)
}else{
query = paste('select distinct ', input$firstAxe, ' from Moore order by ', input$firstAxe, sep='')
choicesValues = sqldf(query)
selectInput(inputId = "firstAxeOutput", label="", choices = choicesValues[[1]], multiple = TRUE, selectize = FALSE)
}
}
})
###############
### second Axis
precAxeRes1 =reactive(paste(input$firstAxeOutput, collapse = "','"))
output$type1 = renderPrint(precAxeRes1())
output$secondAxe= renderUI({
firstSel = input$firstAxe
ContextElements = setdiff(ContextElements, firstSel)
selectInput(inputId = "secondAxe", label = h4("Second Axis"),
choices=c(Choose='',ContextElements), multiple = FALSE)
})
# output Query
output$secondAxeOutput=renderUI({
if(!is.null(input$secondAxe)){
if(input$secondAxe=="" || input$firstAxe==""){
selectInput(inputId = "secondAxeOutput", label="", choices = NULL, multiple = TRUE, selectize = FALSE)
}else{
if(is.null(input$firstAxeOutput)){
query = paste('select distinct ', input$secondAxe, ' from Moore where ',
input$firstAxe, " like ('%') order by ", input$secondAxe , sep='')
# query = paste('select distinct ', input$secondAxe, ' from Moore order by ', input$secondAxe, sep='')
}else{
query = paste('select distinct ', input$secondAxe, ' from Moore where ',
input$firstAxe, " in ('",precAxeRes1(), "') order by ", input$secondAxe , sep='')
}
choicesValues = sqldf(query)
output$query1 = renderPrint(paste("Second Query: ",query))
selectInput(inputId = "secondAxeOutput", label="", choices = choicesValues[[1]], multiple = TRUE, selectize = FALSE)
}
}
})
###############
### third Axis
precAxeRes2 =reactive(paste(input$secondAxeOutput, collapse = "','"))
output$type2 = renderPrint(precAxeRes2())
output$thirdAxe= renderUI({
precSel = c(input$firstAxe, input$secondAxe)
ContextElements = setdiff(ContextElements, precSel)
selectInput(inputId = "thirdAxe", label = h4("Third Axis"),
choices=c(Choose='',ContextElements), multiple = FALSE)
})
# output Query
output$thirdAxeOutput=renderUI({
if(!is.null(input$thirdAxe)){
if(input$thirdAxe=="" || input$secondAxe==""){
selectInput(inputId = "thirdAxeOutput", label="", choices = NULL, multiple = TRUE, selectize = FALSE)
}else{
if(is.null(input$firstAxeOutput) && is.null(input$secondAxeOutput)){
query = paste('select distinct ', input$thirdAxe, ' from Moore where ',
input$firstAxe, " like ('%') and ",
input$secondAxe, " like ('%') order by " , input$thirdAxe,
sep='')
}else if(!is.null(input$firstAxeOutput) && is.null(input$secondAxeOutput)){
query = paste('select distinct ', input$thirdAxe, ' from Moore where ',
input$firstAxe, " in ('",precAxeRes1(), "') and ",
input$secondAxe, " like ('%') order by " , input$thirdAxe,
sep='')
}else if(is.null(input$firstAxeOutput) && !is.null(input$secondAxeOutput)){
query = paste('select distinct ', input$thirdAxe, ' from Moore where ',
input$firstAxe, " like ('%') and ",
input$secondAxe, " in ('",precAxeRes2(), "') order by " , input$thirdAxe,
sep='')
}else{
query = paste('select distinct ', input$thirdAxe, ' from Moore where ',
input$firstAxe, " in ('",precAxeRes1(), "') and ",
input$secondAxe, " in ('",precAxeRes2(), "') order by " , input$thirdAxe,
sep='')
}
choicesValues = sqldf(query)
output$query2 = renderPrint(paste("Third Query: ",query))
selectInput(inputId = "thirdAxeOutput", label="", choices = choicesValues[[1]], multiple = TRUE, selectize = FALSE)
}
}
})
###############
### fourth Axis
precAxeRes3 =reactive(paste(input$thirdAxeOutput, collapse = "','"))
output$type3 = renderPrint(precAxeRes3())
output$fourthAxe= renderUI({
precSel = c(input$firstAxe, input$secondAxe, input$thirdAxe)
ContextElements = setdiff(ContextElements, precSel)
selectInput(inputId = "fourthAxe", label = h4("Fourth Axis"),
choices=c(Choose='',ContextElements), multiple = FALSE)
})
# output Query
output$fourthAxeOutput=renderUI({
if(!is.null(input$fourthAxe)){
if(input$fourthAxe=="" || input$thirdAxe==""){
selectInput(inputId = "fourthAxeOutput", label="", choices = NULL, multiple = TRUE, selectize = FALSE)
}else{
if(is.null(input$firstAxeOutput) && is.null(input$secondAxeOutput) && is.null(input$thirdAxeOutput)){
query = paste('select distinct ', input$fourthAxe, ' from Moore where ',
input$firstAxe, " like ('%') and ",
input$secondAxe, " like ('%') and ",
input$thirdAxe, " like ('%') order by " , input$fourthAxe,
sep='')
# query = paste('select distinct ', input$thirdAxe, ' from Moore order by ', input$thirdAxe, sep='')
}else if(!is.null(input$firstAxeOutput) && !is.null(input$secondAxeOutput) && is.null(input$thirdAxeOutput)){
query = paste('select distinct ', input$fourthAxe, ' from Moore where ',
input$firstAxe, " in ('",precAxeRes1(), "') and ",
input$secondAxe, " in ('",precAxeRes2(), "') and ",
input$thirdAxe, " like ('%') order by " , input$fourthAxe,
sep='')
}else if(!is.null(input$firstAxeOutput) && is.null(input$secondAxeOutput) && !is.null(input$thirdAxeOutput)){
query = paste('select distinct ', input$fourthAxe, ' from Moore where ',
input$firstAxe, " in ('",precAxeRes1(), "') and ",
input$secondAxe, " like ('%') and ",
input$thirdAxe, " in ('",precAxeRes3(), "') order by " , input$fourthAxe,
sep='')
}else if(!is.null(input$firstAxeOutput) && is.null(input$secondAxeOutput) && is.null(input$thirdAxeOutput)){
query = paste('select distinct ', input$fourthAxe, ' from Moore where ',
input$firstAxe, " in ('",precAxeRes1(), "') and ",
input$secondAxe, " like ('%') and ",
input$thirdAxe, " like ('%') order by " , input$fourthAxe,
sep='')
}else if(is.null(input$firstAxeOutput) && !is.null(input$secondAxeOutput) && !is.null(input$thirdAxeOutput)){
query = paste('select distinct ', input$fourthAxe, ' from Moore where ',
input$firstAxe, " like ('%') and ",
input$secondAxe, " in ('",precAxeRes2(), "') and ",
input$thirdAxe, " in ('",precAxeRes3(), "') order by " , input$fourthAxe,
sep='')
}else if(is.null(input$firstAxeOutput) && !is.null(input$secondAxeOutput) && is.null(input$thirdAxeOutput)){
query = paste('select distinct ', input$fourthAxe, ' from Moore where ',
input$firstAxe, " like ('%') and ",
input$secondAxe, " in ('",precAxeRes2(), "') and ",
input$thirdAxe, " like ('%') order by " , input$fourthAxe,
sep='')
}else if(!is.null(input$firstAxeOutput) && !is.null(input$secondAxeOutput) && !is.null(input$thirdAxeOutput)){
query = paste('select distinct ', input$fourthAxe, ' from Moore where ',
input$firstAxe, " in ('",precAxeRes1(), "') and ",
input$secondAxe, " in ('",precAxeRes2(), "') and ",
input$thirdAxe, " in ('",precAxeRes3(), "') order by " , input$fourthAxe,
sep='')
}else{
query = paste('select distinct ', input$fourthAxe, ' from Moore where ',
input$firstAxe, " like ('%') and ",
input$secondAxe, " like ('%') and ",
input$thirdAxe, " in ('",precAxeRes3(), "') order by " , input$fourthAxe,
sep='')
}
choicesValues = sqldf(query)
output$query3 = renderPrint(paste("Fourth Query: ",query))
selectInput(inputId = "fourthAxeOutput", label="", choices = choicesValues[[1]], multiple = TRUE, selectize = FALSE)
}
}
})
}
)
)
问候