0
votes

I am trying to build a shiny app where the user can decide how many tabs he wants to be shown. Here's what I have so far:

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)

This does not produce the desired results, as the comparison tabs are not shown properly. I have already checked out these 2 threads: R Shiny - add tabPanel to tabsetPanel dynamically (with the use of renderUI) R Shiny dynamic tab number and input generation but they don't seem to solve my problem. Yes, they create tabs dynamically with a slider, but they don't allow to fill these with content as far as I can tell.

1

1 Answers

1
votes

What works for me is a combination for lapply and do.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)