【发布时间】:2019-05-07 21:56:21
【问题描述】:
我想在我的应用程序中动态形成选项卡,即当我从下拉菜单中选择选项时,一旦我选择一个选项,就会出现一个具有相同名称的选项卡。选择输入显示的选项来自我的数据库。为了让场景更清晰,我附上了动态显示选项卡的示例应用程序,稍后我将附上我的代码 这是我们的示例应用程序:
library(shiny)
ui <- (fluidPage(
titlePanel("Demonstration of renderUI in shiny - Dymanically creating the tabs based on user inputs"),
sidebarLayout(
sidebarPanel(
# Numeric input to enter the number of tabs needed
numericInput("n", 'Enter the number of tabs needed', 1)
),
mainPanel(
uiOutput('tabs')
)
)
))
server <- (function(input,output){
output$tabs = renderUI({
Tabs <- lapply(paste("tab no.", 1:input$n, sep=" "), tabPanel)
do.call(tabsetPanel, Tabs)
})
})
shinyApp(ui, server)
这里的选项卡根据增加和减少的数字而增加,我有下拉菜单,它将显示我的数据库表的元组,如下所示
| name |
+----------------------+
| aaa |
| kart |
这是我的错误代码:
library("shiny")
library("shinydashboard")
library("pool")
library("DBI")
pool <- dbPool(drv = RMySQL::MySQL(),dbname = "demo",host = "db.cr7dht.us-east-2.rds.amazonaws.com",username = "kak",password = "1278", port = 3306)
mychoices = dbGetQuery(pool,"select available_scenario from sc;")
ui <- (fluidPage(
titlePanel("Demonstration of renderUI in shiny - Dymanically creating the tabs based on user inputs"),
sidebarLayout(
sidebarPanel(
selectInput('n', "available scenarios", choices = mychoices, multiple = TRUE),
verbatimTextOutput("selected")
),
mainPanel(
uiOutput('tabs')
)
)
))
server <- (function(input,output,session){
output$tabs = renderUI({
observe({
updateSelectInput(
session, "n", choices = mychoices
)
})
Tabs <- lapply(paste("tab name", 1:input$choices, sep=" "), tabPanel)
do.call(tabsetPanel, Tabs)
})
})
shinyApp(ui, server)
【问题讨论】:
-
在您自己的代码中,
input$choices是一个 字符 向量。所以1:input$choices会抛出一个错误。请改用1:length(input$choices)。 insertTab 也可能对你有用
标签: mysql drop-down-menu shiny tabs