简体   繁体   English

R Shiny SQL反应式查询(不显示selectInput)

[英]R Shiny SQL reactive query (selectInput not displaying)

I would like to build a shiny app which will allow user to choose the table name from database, and their further plotting etc. I stuck at the point of retrieving the table names from database. 我想构建一个闪亮的应用程序,该应用程序将允许用户从数据库中选择表名,并进行进一步的绘制等。我坚持从数据库中检索表名。 I cannot use the tableList which i have created using dbListTables(con,schema="K") as a choice for the selectInput widget. 我不能使用tableList我所使用创建dbListTables(con,schema="K")作为一种选择selectInput插件。 I do not get any error or warning, widget just does not appear at all. 我没有收到任何错误或警告,小部件根本没有出现。

My code: 我的代码:

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))

Thanks for any tipps 谢谢你的小费

[SOLVED] the part for the selectizeInput i solved by placing it on the server side: [已解决] 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))

I additionally made the reactive SQL query. 我还进行了反应式SQL查询。 Than i choosed the table from selectizeInput to display, [NOT SOLVED] however it shows me an error: 比我从selectizeInput选择要显示的表, [未解决]但是它显示了一个错误:

Error in .oci.GetQuery(conn, statement, data = data, prefetch = prefetch,  : 
  ORA-00903: invalid table name

Than smthg has to be wrong with my SQL Query (Thanks for the tipps here!) How its even possible if i choosed the table name from the dbListTables ? 比smthg的SQL查询错误(感谢这里的技巧!)如果我从dbListTables选择表名,怎么可能呢? Any ideas? 有任何想法吗?

I have solved my second question ! 我已经解决了第二个问题 The problem was very small on the side of ui , instead of dataTableOutput , i had tableOutput , so the ui should look like this: ui ,问题非常小,而不是dataTableOutput ,而我有tableOutput ,因此ui应该如下所示:

ui_panel <- 
  tabPanel("Test",
           sidebarLayout(
             sidebarPanel( 
             ),
             mainPanel(
               selectizeInput("tabnames",label = "server side", choices = NULL),
               tableOutput("out"),
               dataTableOutput("table")
             )
           )
  )

Thanks for all the help! 感谢您的所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM