【问题标题】:Dynamic Tab creation with content使用内容创建动态选项卡
【发布时间】:2019-06-18 12:04:27
【问题描述】:

我正在尝试构建一个闪亮的应用程序,用户可以在其中决定要显示多少个标签。到目前为止,这是我所拥有的:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(glue)

ui <- dashboardPage(
  dashboardHeader(),

  dashboardSidebar(
    sliderInput(inputId = "slider", label = NULL, min = 1, max = 5, value = 3, step = 1)
  ),

  dashboardBody(
    fluidRow(
      box(width = 12,
          p(
            mainPanel(width = 12,
                      column(6,
                             uiOutput("reference")
                      ),
                      column(6,
                             uiOutput("comparison")
                      )
            )
            )
      )
    )
  )
)

server <- function(input, output) {

  output$reference <- renderUI({
    tabsetPanel(
    tabPanel(
      "Reference",
      h3("Reference Content"))
    )


  })

  output$comparison <- renderUI({

    req(input$slider)


    tabsetPanel(

      lapply(1:input$slider, function(i) {

      tabPanel(title = glue("Tab {i}"),
               value = h3(glue("Content {i}"))
               )

      })
    )

  })

}

shinyApp(ui = ui, server = server)

这不会产生预期的结果,因为比较选项卡未正确显示。 我已经检查了这两个线程: R Shiny - add tabPanel to tabsetPanel dynamically (with the use of renderUI) R Shiny dynamic tab number and input generation 但他们似乎没有解决我的问题。是的,他们使用滑块动态创建选项卡,但据我所知,它们不允许用内容填充这些选项卡。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    对我有用的是 lapplydo.call 的组合

    library(shiny)
    library(shinydashboard)
    library(shinydashboardPlus)
    library(glue)
    
    ui <- dashboardPage(
        dashboardHeader(),
    
        dashboardSidebar(
            sliderInput(inputId = "slider", label = NULL, min = 1, max = 5, value = 3, step = 1)
        ),
    
        dashboardBody(
            fluidRow(
                box(width = 12,
                    p(
                        mainPanel(width = 12,
                                  column(6,
                                         uiOutput("reference")
                                  ),
                                  column(6,
                                         uiOutput("comparison")
                                  )
                        )
                    )
                )
            )
        )
    )
    
    server <- function(input, output) {
    
        output$reference <- renderUI({
            tabsetPanel(
                tabPanel(
                    "Reference",
                    h3("Reference Content"))
            )
    
    
        })
    
    
        output$comparison <- renderUI({
            req(input$slider)
    
                myTabs = lapply(1:input$slider, function(i) {
    
                    tabPanel(title = glue("Tab {i}"),
                             h3(glue("Content {i}"))
                    )
                })
    
                do.call(tabsetPanel, myTabs)
    
        })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 感谢您的回答。但是,单击选项卡时是否实际显示该值?因为它不适合我,尽管选项卡本身确实可以正确呈现。
    • 没看到,更新了代码。您将文本存储在此处不需要的变量中。如果我的回答有帮助,请不要忘记投票和/或接受它;-)
    猜你喜欢
    • 2020-07-30
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 2019-02-13
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多