Search code examples
rshinydashboardtabpanel

Adding custom number of tabPanels to a tabsetPanel in shiny dashboard


I'm trying to make a tabsetPanel with a number of tabPanels based on a variable form. I've tried the code below, but got some error:

Error in attr(x, "selected") <- TRUE : trying to specify an attribut in a NULL

Some one could aid me, please?

library(shinydashboard)
library(tidyverse)

form <- 2

ui <- dashboardPage(
    title = "Rolê de Aventura", skin="blue",
    dashboardHeader(titleWidth = 1024,
                    title=list(title=tags$img(src="LogoPQ.png",
                                              heigth=45, width=45,
                                              align="left"),
                               title=tags$p(style="text-align:center;",
                                            "Rolê de Aventura")
                    )
    ),
    dashboardSidebar(
        selectInput("categoria", label = "Categoria:",
                    choices = list("Quarteto Misto",
                                   "Dupla Masculina",
                                   "Dupla Mista",
                                   "Dupla Feminina"), width="200px"
        )
    ),
    dashboardBody(
        textInput("equipe", "Nome da equipe:", width = NULL),
        tabsetPanel(width = NULL, type = "tabs",
                    do.call(tabsetPanel,c(width=NULL, type="tabs",
                                           lapply(1:form, function(i) {
                                               tabPanel(title = str_c("Atleta", i),
                                                        textInput(str_c("atleta", i), "Nome:", width=NULL),
                                                        dateInput(str_c("at.nasc", i), "Nascimento:", width="30%"),
                                                        checkboxGroupInput(str_c("at.sex", i), "Sexo:", width="30%",
                                                                           choices=list("Masculino", "Feminino")))
                                           })
                    )
                    )
        )
    )
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

Solution

  • I find useful in this cases to use exec() function with !!!.

    library(shinydashboard)
    library(tidyverse)
    library(shiny)
    
    form <- 2
    
    #store the ui code in a list to use later.
    
    tabs <- map(1:form, function(i) {
              tabPanel(title = str_c("Atleta", i),
                       textInput(str_c("atleta", i), "Nome:"),
                       dateInput(str_c("at.nasc", i), "Nascimento:", width="30%"),
                       checkboxGroupInput(str_c("at.sex", i), "Sexo:", width="30%",
                                          choices=c("Masculino", "Feminino")))
            })
    
    ui <- dashboardPage(
      title = "Rolê de Aventura", skin="blue",
      dashboardHeader(titleWidth = 1024,
                      title=list(title=tags$img(src="LogoPQ.png",
                                                heigth=45, width=45,
                                                align="left"),
                                 title=tags$p(style="text-align:center;",
                                              "Rolê de Aventura")
                      )
      ),
      dashboardSidebar(
        selectInput("categoria", label = "Categoria:",
                    choices = list("Quarteto Misto",
                                   "Dupla Masculina",
                                   "Dupla Mista",
                                   "Dupla Feminina"), width="200px"
        )
      ),
      dashboardBody(
        textInput("equipe", "Nome da equipe:"),
        exec(tabsetPanel, !!!tabs))
        )
    
    
    server <- function(input, output) {
    }
    
    shinyApp(ui = ui, server = server)