通过日期选择基础上的闪亮应用程序从SQL Server获取数据

时间:2017-10-10 11:08:14

标签: sql-server r odbc shiny shiny-server

Hey Every One我正在开发一个Shiny应用程序,我们通过在Application中选择Date来从sql Server通过ODBC Connector提取数据。我无法确定问题所在,因为如果我在R studio上独立执行代码,我可以从sql Server中提取数据但是当在Shiny Environment中执行相同的代码时,我无法在这里实现闪亮的数据以下是亲切的指导我,谢谢。

# ---------------------ui Code -----------------------------
library(shiny) 

shinyUI(pageWithSidebar( 

  headerPanel("Time Analytics"), 

  sidebarPanel( 


      dateRangeInput(inputId = "dateRange",  
                     label =  "Date range",
                     start = "2007-09-17",
                     max = Sys.Date()
      )


  ),#sidebar Panel Ends


    # 09-Main Panel ----
  mainPanel(
    tabsetPanel(id ="theTabs",
                tabPanel("Summary", dataTableOutput("tabi"),textOutput("tabii")) 

    )
  )#Main Panel Ends
))

#------------------Server ----------------------------------



library(shiny);library(sqldf)
library(plyr);library(RODBC)
library(ggplot2)


#Creating the connection 

shinyServer(function(input, output, session){ # pass in a session argument






  # prep data once and then pass around the program

  passData <- reactive({




    ch = odbcConnect("Test")  
    #qry <- "SELECT * FROM Nifty50"
    #qry <- cat("SELECT * FROM Nifty50 WHERE Date >= ",as.date(input$dateRange[1])," AND Date <= ",input$dateRange[2])
    qry <- paste("SELECT * FROM Nifty50 WHERE Date >= ",input$dateRange[1]," AND Date <= ",input$dateRange[2])
    #paste("SELECT * FROM Nifty50 WHERE Date >= ",input$dateRange[1]," AND Date <= ",input$dateRange[2])
    subset_Table <- sqlQuery(ch,qry)

    odbcClose(ch)
    subset_Table <- as.data.frame(subset_Table)
    return(subset_Table)

  })

  output$tabi <- renderDataTable({

    d<- as.data.frame(passData())
    d
  })

  output$tabii <- renderText({

    paste("Minimium Data :",input$dateRange[1], "Max Date:",input$dateRange[2])
  })




  # ----------------------------------------------------End

})

这里的任务是我需要在Date to和From标准的基础上从选定的表中获取数据,这将是根据闪亮app中所选日期的子集数据。

1 个答案:

答案 0 :(得分:1)

修改qry,如下所示:

qry <- paste("SELECT * FROM Nifty50 WHERE Date >= '", input$dateRange[1], "' AND Date <= '", input$dateRange[2], "'", sep = "")