我想在我的应用程序中动态形成选项卡,即当我从下拉菜单中选择选项时,一旦我选择一个选项,就会出现一个具有相同名称的选项卡。选择输入显示的选项来 self 的数据库。为了使场景清晰,我附加了动态显示选项卡的示例应用程序,稍后我将附加我的代码 这是我们的示例应用程序:
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)
最佳答案
首先,如果 n
为 NULL
,那么您不需要 tabpannel,因此我添加了 is.null
检查。其次,在生成选项卡时删除 1:
。粘贴功能会自动将您的inpu$n
转换为多个字符串。无需插入范围。
此外,您不需要将 updateSelectInput
放入 renderUI
函数中。
工作示例
mychoices = 1:10
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({
if(!is.null(input$n)){
Tabs <- lapply(paste("tab name", input$n, sep=" "), tabPanel)
do.call(tabsetPanel, Tabs)}
})
})
关于mysql - 使用从 Shiny 的选择输入功能中选择的名称创建选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56021492/