Import Data from SQL Server To shiny app

↘锁芯ラ 提交于 2019-12-07 06:02:25

This should do what you want.

library(RODBCext)
library(shiny)

ui <- shinyUI(

  pageWithSidebar(

    headerPanel("Hide Side Bar example"),
    sidebarPanel(
      textInput("CATEGORY", "Enter CATEGORY below"),
      submitButton(text="Submit")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Data", tableOutput("tbTable"))
      )

    )
  )
)

server <- function(input, output, session)    
{ # NOTE THE BRACE HERE
  myData <- reactive({
    req(input$CATEGORY)

    #connect to database 
    dbhandle = odbcDriverConnect('driver={SQL Server};server=Server_Name;database=Database_Name;trusted_connection=true')

    #build query
    query = "SELECT * FROM [Your_Table] where [CATEGORY] = ?"

    #store results
    res <- sqlExecute(channel = dbhandle, 
                      query = query,
                      data = list(input$CATEGORY),
                      fetch = TRUE,
                      stringsAsFactors = FALSE) 

    #close the connection
    odbcClose(dbhandle)
    #return results
    res
  })

  output$tbTable <- 
    renderTable(myData())

} # AND NOTE THE CLOSING BRACE HERE

shinyApp(ui = ui, server = server)

You may want to consider this as well.

library(shiny)
library(RODBCext)

shinyApp(
  ui = 
    shinyUI(
      fluidPage(
        uiOutput("select_category"),
        tableOutput("display_data")
      #   plotOutput("plot_data")
      )
    ),

  # server needs the function; looks ok
  server = shinyServer(function(input, output, session)
    {
    # A reactive object to get the query. This lets you use
    # the data in multiple locations (plots, tables, etc) without
    # having to perform the query in each output slot.
    QueriedData <- reactive({
        req(input$showDrop)
        ch <- odbcDriverConnect("driver={SQL Server};server=Server_Name;database=DATABASE_NAME;trusted_connection=true")
        showList <- sqlExecute(ch, "SELECT * FROM [Your_Table] WHERE Category = ?",
                               data = list(Category = input$showDrop),
                               fetch = TRUE,
                               stringsAsFactors = FALSE)
        odbcClose(ch)
        showList
      })

    # The select input control.  These can be managed dynamically 
    # from the server, and then the control send back to the UI
    # using `renderUI`
     output$select_category <- renderUI({
         ch <- odbcDriverConnect("driver={SQL Server};server=Server_Name;database=DATABASE_NAME;trusted_connection=true")
         showList <- sqlExecute(ch, "Select Distinct Category From [Your_Table] Order by Category", 
                                fetch = TRUE,
                                stringsAsFactors = FALSE)
         odbcClose(ch)
         selectInput(inputId = "showDrop",
                     label = "Select Asset",
                     showList$Category)
       })

    # Display the data in a table
    output$display_data <- renderTable({
        QueriedData()
      })

    # Display a plot
    # output$plot_data <- 
    #  renderPlot({
    #    plot(QueriedData()) # fill in the plot code you want to use.
    #  })

  })
)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!