shinydo.call

Do.call function with R shiny and navlistPanel and renderUi


I try to build a shiny web app with a navlistPanel on the left and contents page on the right. I also want an Id to use the value of the selected tabPanel as an input. Also i want a navlistPanel design. To do so, i found this construction :

ui <- fluidPage(
  sidebarPanel(
      navlistPanel(
         id="id_panel",
         widths=c(12,12),
         tabPanel(title="tab1",value="p1"),
         tabPanel(title="tab2",value="p2"),
         tabPanel(title="tab3",value="p3"),
       )
  ),
  mainPanel(tags$h2("hello"))
     )

server <- function(input, output) {}

shinyApp(ui, server)

Except I want that the number of tabPanel depends on the returns of a previous function. I try so to use a renderUI but I got the error when I try to put the widths argument whereas it seems to be ok for the ID argumentd. The below code do not work :

ui <- fluidPage(
  sidebarPanel(
      uiOutput("multipleUI")
      ),
  mainPanel(tags$h2("hello"))
     )


server <- function(input, output) {
  output$multipleUI <- renderUI({
    tabs <- list(NULL)
    for(i in 1:3){
      tabs[[i]] <- tabPanel(title=(paste0("tab",i)), value=paste0("p",i))
    }
    do.call(navlistPanel,c(tabs,id="tab_number",widths=c(12,12)))
  })
  
}    

shinyApp(ui, server)

Any idea to the right way to do that? Thanks


Solution

  • Add a list() outside your widths values

    ui <- fluidPage(
        sidebarPanel(
            uiOutput("multipleUI")
        ),
        mainPanel(tags$h2("hello"))
    )
    
    
    server <- function(input, output) {
        output$multipleUI <- renderUI({
            tabs <- list(NULL)
            for(i in 1:3){
                tabs[[i]] <- tabPanel(title=(paste0("tab",i)), value=paste0("p",i))
            }
            do.call(navlistPanel,c(tabs,id="tab_number",widths=list(c(12,12))))
        })
        
    }    
    
    shinyApp(ui, server)