shinyApp与mysql数据库

时间:2016-07-23 08:28:13

标签: sql r shiny shiny-server

附加了

shinyApp代码。我正在使用来自mysql数据库的数据,并希望根据查询和str以及数据摘要显示数据表。代码运行正常。我是否需要一次又一次地连接到数据库以在我的闪亮应用程序中显示str,summary和table?

library(shiny)
library(DBI)
library(RMySQL)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("dataset", "Choose a dataset:", 
                  choices = c("city", "country","countrylanguage")),
      numericInput("obs", "Number of observations to view:", 10),
      submitButton("Update View")
    ),
    mainPanel(
      tabsetPanel(tabPanel("Table",tableOutput("view")),
                  tabPanel("Str",verbatimTextOutput("dump")),
                  tabPanel("Summary",verbatimTextOutput("stats"))
      ))
  )
)
# Define server logic required to summarize and view the 
# selected dataset
server<-function(input, output) {
  output$view <- renderTable({
    conn <- dbConnect(drv = RMySQL::MySQL(),dbname = "world",host = "localhost",username = "root",password = "xxxx")
    on.exit(dbDisconnect(conn), add = TRUE)
    head(dbReadTable(conn = conn,  name = input$dataset), n = input$obs)
  })
  output$dump <- renderPrint({
    conn <- dbConnect(drv = RMySQL::MySQL(),dbname = "world",host = "localhost",username = "root",password = "xxxx")
    on.exit(dbDisconnect(conn), add = TRUE)
    str(dbReadTable(conn = conn,  name = input$dataset))
  })
  output$stats <-renderPrint({
    conn <- dbConnect(drv = RMySQL::MySQL(),dbname = "world",host = "localhost",username = "root",password = "xxxx")
    on.exit(dbDisconnect(conn), add = TRUE)
    summary(dbReadTable(conn = conn,  name = input$dataset), n = input$obs)
  })
  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset
    )})
}
shinyApp(ui, server)

0 个答案:

没有答案