chungkim271
chungkim271

Reputation: 967

dynamically generated navbar and tabPanels in r shiny

I'm trying to make a dynamically generated navbar based on the session user id.

I have a data table that maps the session user to a list of that user's clients. I want the app to produce a navbar where each tabPanel is for each client that user has. I'm not sure how I can easily do that since navbarPage() doesn't take a list argument.

Below is my example

library(shiny)


data <- data.frame(user=c("emily", "emily"), clients=c("client1", "client2"))

CreateCustomNavbarContent <- function(data) {

    l <- lapply(data$clients, function(client) {
        tabPanel(client, 
            h2(client))
    })

    renderUI({
        l
    }) 

}

shinyApp(
  ui <- fluidPage(
    uiOutput("custom_navbar")
  ),

  server <- function(input, output) {

      output$custom_navbar <- renderUI({

          ## commented below doesn't work

          # navbarPage(
          #     CreateCustomNavbarContent(data)
          # )

          navbarPage("",
              tabPanel("client1", 
                  h2("client1")
              ),
              tabPanel("client2",
                  h2("client2")
              )
          )

      })

  }
)

Upvotes: 1

Views: 434

Answers (1)

Florian
Florian

Reputation: 25375

You could achieve what you want with do.call, so we can pass a list of arguments as separate arguments. Below is a working example, I gave emily a companion called John so you can validate that the code does what you want ;)

Hope this helps!

library(shiny)

data <- data.frame(user=c("Emily", "Emily","John","John"), clients=c("client1", "client2","client3","client4"))

ui = fluidPage(
  selectInput('select_user','Select user:',unique(data$user)),
  uiOutput('mytabsetpanel')  

)
server = function(input, output, session){
  output$mytabsetpanel = renderUI({
    myTabs = lapply(data$clients[data$user==input$select_user], tabPanel)
    do.call(tabsetPanel, myTabs)
  })
}
shinyApp(ui,server)

Upvotes: 1

Related Questions