R 动态查询与闪亮的应用程序
我想构建一个连接到数据库的闪亮应用程序,我的问题是如何动态创建查询,这里我举了一个例子,其中用户有四个研究轴,您只需要在R环境中复制代码,但在我的应用程序中,我有12个研究轴,如果您有想法,我不知道如何管理查询R 动态查询与闪亮的应用程序,r,shiny,R,Shiny,我想构建一个连接到数据库的闪亮应用程序,我的问题是如何动态创建查询,这里我举了一个例子,其中用户有四个研究轴,您只需要在R环境中复制代码,但在我的应用程序中,我有12个研究轴,如果您有想法,我不知道如何管理查询 # rm(list=ls()) library(sqldf) library(car) data(Moore) ContextElements = colnames(Moore) ContextElements[1] = 'partner_status' library(shiny) r
# 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)
}
}
})
}
)
)
这是一个来自反应式窗口小部件的动态查询示例。我希望这对某人有所帮助:
不太清楚你在问什么。你只是想看看如何简化你的代码,还是你正在排除一个特定的错误?我要求简化我的代码,因为在我的实际应用中,我有12个研究轴,在我的例子中,我可以测试所有的可能性,因为我只有4个轴。正如您所看到的,我在代码中每次都重复查询,我想创建一个全局查询,并且每次用户选择一个axis==>global query+新的axis选项时,请不要发布可能损坏读取器工作区的代码。@G.Grothendieck,我的代码的损坏在哪里?这只是一个例子,您需要R、Shiny和sqldf包