Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何存储来自反应式SQL查询的数据,以便进一步过滤(DT)并显示在ggplot中_R_Database_Datatable_Dt_Roracle - Fatal编程技术网

R 如何存储来自反应式SQL查询的数据,以便进一步过滤(DT)并显示在ggplot中

R 如何存储来自反应式SQL查询的数据,以便进一步过滤(DT)并显示在ggplot中,r,database,datatable,dt,roracle,R,Database,Datatable,Dt,Roracle,如何将从数据库接收到的数据存储为对象,进一步(如果需要)使用datatable(DT)进行过滤,并使用ggplot显示 以下是代码: library(shiny) library(ROracle) library(DT) ui <- shinyUI(navbarPage("Test", tabPanel("Test", sidebarLayout(

如何将从数据库接收到的数据存储为对象,进一步(如果需要)使用datatable(
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)

干杯

我试过了
sqlOutput我不太明白你的意思?我还想,在
dbGetQuery
之后,我会得到简单的数据。frame..如果是这样,为什么我不能使用
data\u filt-Ups显示它,我会在复制时监督它..现在要更新它仍然不起作用,我使用了
print
,而且表似乎根本没有更新自己(在选择了一些过滤器之后)可能是因为
tabelle\u rows\u all
——未过滤任何内容。你在这里需要什么<代码>隔离(sqlOutput())
output$tabelle <- DT::renderDataTable({
            input$button1
            datatable(isolate(sqlOutput()), rownames=TRUE, filter="top", options=list(pageLength=10))})