in R shiny, how to automatically or based on function tabPanel, given we have 3 levels of lists?

后端 未结 1 1555
清歌不尽
清歌不尽 2020-12-21 14:39

I need to create conditional 3 levels of tabs the first level or tabPanel includes three tabs \"NUTS\",\"SWEETS\",\"DRINKS\" so the

level1<-list(DRINKS,SW

相关标签:
1条回答
  • 2020-12-21 15:01
    hotdrinks<-list("tea","green tea") 
    juices<-list("orange","mango") 
    energydrinks<-list("powerhorse","redbull") 
    drinks<-list("hotdrinks"=hotdrinks, "juices"=juices, "energydrinks"=energydrinks) 
    
    lst_drinks <- lapply(seq_along(drinks), 
                         #browser()
                         #create 2nd level, tab name with the corresponding 3rd level list  
                         function(x) tabPanel(names(drinks[x]),
                                              #create tabsetPanel for hdrinks, jcs, ngdrinks level i.e. 3rd level 
                                              do.call("tabsetPanel", 
                                                      lapply(drinks[[x]], function(y) tabPanel(y))
                                                      )
                                              )
                         )
    
    hdrinks<-  
      tabsetPanel(
        tabPanel("tea"),
        tabPanel("green tea")
      )
    jcs<-  
      tabsetPanel(
        tabPanel("orange"),
        tabPanel("mango")
      )
    ngdrinks<-  
      tabsetPanel(
        tabPanel("powerhorse"),
        tabPanel("redbull")
      )
    
    runApp(list(
      ui = shinyUI(fluidPage(
        sidebarLayout( 
          sidebarPanel(width = 2),      
          mainPanel(tabsetPanel(id='conditioned',
                                tabPanel("drinks",value=3,
                                         tabsetPanel(
                                           tabPanel("hotdrinks",
                                                    #No need for tabsetPanel as hdrinks already has one, therefore I removed it in lapply
                                                    tabsetPanel(hdrinks)),
                                           tabPanel("juices",
                                                    tabsetPanel(jcs)),
                                           tabPanel("energydrinks",
                                                    tabsetPanel(ngdrinks))
    
                                         )),
                                tabPanel("drinks-test",
                                         do.call("tabsetPanel", lst_drinks))
                                         ))
        ))),
    
      server = function(input, output, session) {}
    ))
    

    The Full solution

    hotdrinks<-list("tea","green tea") 
    juices<-list("orange","mango") 
    energydrinks<-list("powerhorse","redbull") 
    drinks<-list("hotdrinks"=hotdrinks,"juices"=juices,"energydrinks"=energydrinks) 
    
    biscuits<-list("loacker","tuc") 
    choc<-list("aftereight","lindt") 
    gum<-list("trident","clortes") 
    sweets<-list("gum"=gum,"biscuits"=biscuits,"choc"=choc)
    
    all <- list("drinks"=drinks, "sweets"=sweets)
    
    all_lst <- lapply(seq_along(all), function(z) tabPanel(names(all)[z], 
                                                           do.call("tabsetPanel", 
                                                                   lapply(seq_along(all[[z]]), function(x) tabPanel(names(all[[z]][x]), 
                                                                                                                    do.call("tabsetPanel", 
                                                                                                                            lapply(all[[z]][[x]], function(y) tabPanel(y, DT::dataTableOutput(y)))
                                                                                                                            )
                                                                                                                    )
                                                                          )
                                                                   )
                                                           )
                      )
    
    runApp(list(
      ui = shinyUI(fluidPage( 
        sidebarLayout( 
          sidebarPanel(width = 2),      
          mainPanel(do.call("tabsetPanel", c(id='conditioned', all_lst)))
          ))),
      server = function(input, output, session) {
        observe({
          nms = unlist(all)
          names(nms) <- sub('\\d', '', names(nms))
          for(i in seq_along(nms)){
            #browser()
            local({
              nm      = nms[i]
              CAT_var = unlist(strsplit(names(nm), '\\.'))[1]
              PN_var  = unlist(strsplit(names(nm), '\\.'))[2]
              SP_var  = nm[[1]]
              output[[SP_var]] <- DT::renderDataTable({filter(t1, CAT==CAT_var, PN==PN_var, SP==SP_var)})
            })
          }
        })
      }
    ))
    
    0 讨论(0)
提交回复
热议问题