【发布时间】:2018-04-12 15:37:42
【问题描述】:
我有一个包含多个表的 MySQL 数据库。现在我想在 Shiny 仪表板中创建一个下拉菜单,它会根据表的每一列的唯一值自动添加值。
我当前的代码如下所示
ui <- fluidPage(
numericInput("nrows", "Enter the number of rows to display:", 5),
tableOutput("tbl")
)
server <- function(input, output, session) {
output$tbl <- renderTable({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "apilogs",
host = "localhost",
username = "root",
password = "root")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0("SELECT * FROM logs where key = 'agc' LIMIT ", input$nrows, ";"))
})
}
现在,对于我闪亮的仪表板,我想根据日志表列的值创建一个下拉菜单。
dashboardSidebar(
selectInput("Filter", "Filter:",
choices = c())
)
现在在choices 中,我想根据表格列动态获取选择。我该怎么做。
【问题讨论】:
-
使用反应函数将列值放在那里,使用
return(x)返回值列表然后创建一个观察函数并使用更新选择输入,将反应()放在选择参数上,你会得到你想要的。