【问题标题】:Creating Tabs with the name selected from select input function in shiny使用从闪亮的选择输入函数中选择的名称创建选项卡
【发布时间】: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


【解决方案1】:

首先,如果nNULL,你不想要一个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)}

  })
})

【讨论】:

    猜你喜欢
    • 2018-08-02
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    相关资源
    最近更新 更多