如何使用MySQL数据库动态生成Shiny仪表板中的下拉菜单?

时间:2018-04-10 09:02:00

标签: mysql r shiny shinydashboard

我有一个包含多个表的MySQL数据库。现在,我想在Shiny仪表板中创建一个下拉菜单,该菜单会根据表格中每列的唯一值自动添加值。

我连接数据库的代码是

    library(RMySQL)

options(mysql = list(
 "host" = "localhost",
 "port" = 3306,
 "user" = "root",
 "password" = "root"
))

databaseName <- "Apilogs"
table <- "Eventlog"


loadData <- function() {
    # Connect to the database
    db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host,
      port = options()$mysql$port, user = options()$mysql$user,
      password = options()$mysql$password)
    # Construct the fetching query
    query <- sprintf("SELECT * FROM eventlog", table)
    # Submit the fetch query and disconnect
    data <- dbGetQuery(db, query)
    dbDisconnect(db)
    data
}

现在我在侧栏中有我的下拉列表

        selectInput("region", "Region:",
              choices = colnames("I WANT TO POPULATE THIS FROM THE DB"))

我该怎么办呢。

0 个答案:

没有答案