How to make a function in a for loop or lapply loop in a tabItem dashboard shiny

后端 未结 1 1157
刺人心
刺人心 2021-01-23 10:19

I\'m making a ShinyDashboard program and I have some troubles in finding a way to make a loop in the dashboardBody to catch MenuItems. This is a simple example of what I\'m tryi

相关标签:
1条回答
  • 2021-01-23 10:34

    The problem in your code is, that you try to use a list of tabItem objects as an argument for tabItems, but this is invalid according to the documentation of tabItems.

    tabItems(...)

    ... Items to put in the container. Each item should be a tabItem.

    do.call can be used to resolve this issue. Basically, do.call operates as follows.

    add <- function(x, y){x + y}
    do.call(add, list(4, 3)) # same as add(4, 3)
    ## 7
    

    So you basically want to use the list returned from lapply as the second argument to do.call while the first argument is the function to call (tabItems).

    output$test_UI <- renderUI ({
      items <- c(
        list(tabItem(tabName = "menu1_tab", uiOutput("menu1_UI"))),
        lapply(1:5, function(i){
          tabItem(tabName = VecNames[i], uiOutput(paste0("Menu",i)))        
        })
      )
      do.call(tabItems, items)
    })
    
    0 讨论(0)
提交回复
热议问题