0

我如何将从数据库接收到的数据存储为对象,进一步(如果需要)将使用数据表(DT)过滤并使用显示ggplot

这是代码:

library(shiny)
library(ROracle)
library(DT)
ui <- shinyUI(navbarPage("Test",
                   tabPanel("Test",
                            sidebarLayout(
                              sidebarPanel(
                                textInput("drv", "Database Driver", value="Oracle"),
                                textInput("user", "User ID"),
                                passwordInput("passwd", "Password"),
                                actionButton("connectDB", "Connect to DB")) ,
                              mainPanel(
                                textOutput("test"),
                                wellPanel(
                                uiOutput("tabnames_ui"),
                                uiOutput("columnnames_ui"),
                                actionButton("button1", "Select")),
                                plotOutput("plot"),
                                dataTableOutput("tabelle")
                              ))
                   )
))


server <- shinyServer(function(input, output, session) {
  con=reactiveValues(cc=NULL)

  observeEvent(input$connectDB,{
    if(input$drv != "Oracle"){
      con$cc="Only 'Oracle' implemented currently"
    }else{
      drv <- dbDriver("Oracle")
      con$cc<- dbConnect(drv,"xxx/x",username=input$user,password=input$passwd) 
    }
  })
  observe({
    if(!is.null(con$cc)& is(con$cc,"OraConnection")){ # check if connected
      output$test <- renderText({
        "connection success"
      })
      tableList <-reactive({
        dbListTables(con$cc,schema="K")
      }) 

      columnList <-reactive({
        dbListFields(con$cc, name=input$tabnames, schema = "K")
      }) 


      output$tabnames_ui=renderUI({selectInput("tabnames",label = "Tabelle:", choices = tableList(), selected="xy")})

      output$columnnames_ui=renderUI({
        selectInput("columnname", label = "Spalten:", choices = columnList(), multiple=TRUE, selected=
                      if(input$tabnames== "xy"){
                         c("DATI_CREATE","BLOCKNR","ORDNR")}
                    else{NULL})
      }) 

      d <- eventReactive(input$button1, { input$tabnames })

      sqlOutput <- reactive({
        sqlInput <- paste("select",paste(input$columnname, collapse = ","), "from K.",d(), "where dati_create between to_date('02.01.2016','dd.mm.yyyy') and to_date('07.01.2016','dd.mm.yyyy')")
        rs <- dbGetQuery(con$cc, sqlInput)
      })

      output$tabelle <- DT::renderDataTable({
        input$button1
        datatable(isolate(sqlOutput()), rownames=FALSE, filter="top", options=list(pageLength=10))})

      output$plot <- renderPlot({

        filtered_data <- input$tabelle_rows_all
        data_filt <- sqlOutput()[filtered_data,] # this row is responsible for empty plot. How i can store the sqlOutput() object so i can further manipulate it?

        ggplot(data_filt, aes(x=ORDNR,y=BLOCKNR)) + geom_line()
      })

      }else if (!is.null(con$cc) ){
      output$test <- renderText({
        con$cc
      })
    }
  })


  session$onSessionEnded(function() { 
    observe({
      if(!is.null(con$cc)& is(con$cc,"OraConnection")){# check if connected
        print(paste0("disconnect ",dbDisconnect(con$cc)))}
    }) 
  })

})

shinyApp(ui=ui, server=server)

谢谢你的帮助!

[已解决] 问题出在rownames=FALSE

所以对于datatable,它应该是这样的:

output$tabelle <- DT::renderDataTable({
            input$button1
            datatable(isolate(sqlOutput()), rownames=TRUE, filter="top", options=list(pageLength=10))})

干杯

4

0 回答 0