[英]R Shiny SQL reactive query (selectInput not displaying)
我想構建一個閃亮的應用程序,該應用程序將允許用戶從數據庫中選擇表名,並進行進一步的繪制等。我堅持從數據庫中檢索表名。 我不能使用tableList
我所使用創建dbListTables(con,schema="K")
作為一種選擇selectInput
插件。 我沒有收到任何錯誤或警告,小部件根本沒有出現。
我的代碼:
library(ROracle)
library(shiny)
server <- shinyServer(
function(input, output, session) {
con <- dbConnect(dbDriver("Oracle"),"xxx/K",username="user",password="pwd")
tableList <- dbListTables(con,schema="K")
output$out <- renderPrint(tableList)
df <- data.frame()
quer <- paste("select * from K.", input$tabnames)
df <- data.frame(dbGetQuery(con, quer))
output$table <- renderTable({df})
session$onSessionEnded(function() { dbDisconnect(con) })
})
ui_panel <-
tabPanel("Test",
sidebarLayout(
sidebarPanel(
),
mainPanel(
selectInput("tabnames","tabnames", choices=as.list(tableList)),
tableOutput("out"),
tableOutput("table")
)
)
)
ui <- shinyUI(navbarPage("Test",ui_panel))
runApp(list(ui=ui,server=server))
謝謝你的小費
[已解決] selectizeInput
的部分我通過將其放在服務器端來解決:
library(ROracle)
library(shiny)
library(DT)
server <- shinyServer(
function(input, output, session) {
con <- dbConnect(dbDriver("Oracle"),"xx/K",username="user",password="pwd")
tableList <- dbListTables(con,schema="K")
updateSelectizeInput(session, "tabnames", server = TRUE, choices = tableList)
sqlOutput <- reactive({
sqlInput <- paste("select * from K.",input$tabnames)
dbGetQuery(con, sqlInput)
})
output$table <- DT::renderDataTable(sqlOutput(), server=TRUE, rownames=FALSE, filter="top", options=list(pageLength=10))
session$onSessionEnded(function() { dbDisconnect(con) })
})
ui_panel <-
tabPanel("Test",
sidebarLayout(
sidebarPanel(
),
mainPanel(
selectizeInput("tabnames",label = "server side", choices = NULL),
tableOutput("out"),
tableOutput("table")
)
)
)
ui <- shinyUI(navbarPage("Test",ui_panel))
runApp(list(ui=ui,server=server))
我還進行了反應式SQL查詢。 比我從selectizeInput
選擇要顯示的表, [未解決]但是它顯示了一個錯誤:
Error in .oci.GetQuery(conn, statement, data = data, prefetch = prefetch, :
ORA-00903: invalid table name
比smthg的SQL查詢錯誤(感謝這里的技巧!)如果我從dbListTables
選擇表名,怎么可能呢? 有任何想法嗎?
我已經解決了第二個問題 ! 在ui
,問題非常小,而不是dataTableOutput
,而我有tableOutput
,因此ui
應該如下所示:
ui_panel <-
tabPanel("Test",
sidebarLayout(
sidebarPanel(
),
mainPanel(
selectizeInput("tabnames",label = "server side", choices = NULL),
tableOutput("out"),
dataTableOutput("table")
)
)
)
感謝您的所有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.