如何在Shiny app中的仪表板主体中输出数据库表?

时间:2018-04-19 10:25:48

标签: r shiny

如何将数据库查询的结果输出到我闪亮的应用程序仪表板中。这是我的代码

library(shiny)
library(shinydashboard)
library(dbConnect)
library(RMySQL)
library(DBI)
library(foreign)

conn <- dbConnect(
    drv = RMySQL::MySQL(),
    dbname = "shinytest",
    host = "localhost",
    username = "root",
    password = "root")

rs <- dbGetQuery(conn, "Select * from test;") 

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody())

server <- function(input, output) {}

shinyApp(ui, server)

现在代码rs <- dbGetQuery(conn, "Select * from test")确实在控制台窗口中提供输出,但我想在仪表板主体中输出它。 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

library(shiny)
library(shinydashboard)
library(dbConnect)
library(RMySQL)
library(DBI)
library(foreign)

dbDisconnect(conn)
conn <- dbConnect(
    drv = RMySQL::MySQL(),
    dbname = "apilogs",
    host = "localhost",
    username = "root",
    password = "root")

rs <- dbGetQuery(conn, "Select * from test;") 

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(box(

tableOutput('table')
)))


server <- function(input, output) {
output$table <- renderTable(rs)

}

dbDisconnect(conn)
shinyApp(ui, server)