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

问题描述 投票:0回答:1

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

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")确实在控制台窗口中提供输出,但我想在仪表板主体中输出它。我怎样才能做到这一点?

r shiny
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.