在R Shiny中,给定3个级别的列表,如何自动或基于功能tabPanel?

约翰·史密斯

我需要在第一级或tabPanel中创建条件3级标签,第一级或tabPanel包含三个标签“ NUTS”,“ SWEETS”,“ DRINKS”,因此

level1<-list(DRINKS,SWEETS,NUTS)

第二层或以第一层为条件,例如在选择DRINKS(饮料)之后,将是果汁,能量饮料,热饮料。第三层将是在选择能量饮料以“动力马”,“红牛”之后

尝试过的代码但不起作用是这样的

列表------------------------------------------------- ------------------

library(shiny)
library(reshape2)
library(dplyr)

hotdrinks<-list('hotdrinks'=list("tea","green tea")) 
juices<-list('juices'=list("orange","mango") )
energydrinks<-list('energydrinks'=list("powerhorse","redbull")) 
drinks<-list('drinks'=list(hotdrinks,juices,energydrinks))
biscuits<-list('bisc'=list("loacker","tuc"))
choc<-list('choc'=list("aftereight","lindt") )
gum<-list('gum'=list("trident","clortes") )
sweets<-list('sweets'=list(gum,juices,energydrinks))

almonds<-list('almonds'=list("salted","roasted") )
pistcio<-list('pistcio'=list("flavourd","roasted")) 
nuts<-list('nuts'=list(almonds,pistcio))

all_products<-list(sweets,nuts,drinks)
mt<-melt(all_products)

mt2<-mt%>%mutate("Price"=c(23,34,23,23,54,32,45,23,12,56,76,34,62,12,98,43),
          "Quantity"=c(10,20,26,22,51,52,45,23,12,56,76,55,62,12,98,43))

t1<-mt2[,c(5,3,1,8,7)]
t1
colnames(t1)<-c("CAT","PN","SP","Quantity","Price")

t2<-list(unique(t1$CAT))
t2

应用------------------------------------------------- --------------------

library(shiny)

server <- function(input, output,session) {
  observe({print(input$t)})
  observe({print(input$u)})
  observe({print(input$v)})
  t3<-t1%>%filter(t1$CAT==input$t)
  print(t3)
  t4<-list(unique(t3$PN))
  print(t4)
  t5<-t3%>%filter(t3$PN==input$r)
  print(t5)
  t6<-list(unique(t5$SP))
  print(t6)
  t7<-reactive({
         t1%>%filter(t1$CAT==input$t,t1$PN==input$u,t1$SP==inptut$v)
         print(t7())
       })
  output$mytable <- DT::renderDataTable({
         t7
       })

  lapply(1:5, function(j) {
         DT::dataTableOutput("mytable")
       })
}

ui <- pageWithSidebar(
  headerPanel("xxx"),
  sidebarPanel(),
  mainPanel(
    do.call(tabsetPanel, c(id='t',lapply(unlist(t2), function(i) {
  tabPanel(
      do.call(tabsetPanel, c(id='u',lapply(unlist(t4), function(i) {
      tabPanel(
        do.call(tabsetPanel, c(id='v',lapply(unlist(t6), function(i) {
          tabPanel(DT::dataTableOutput("mytable")
              )
        })))

          )
        })))  

      )
    })))

  )
)
shinyApp(ui, server)

手动步骤

hotdrinks<-list("tea","green tea") 
juices<-list("orange","mango") 
energydrinks<-list("powerhorse","redbull") 
drinks<-list(hotdrinks,juices,energydrinks) 

biscuits<-list("loacker","tuc") 
choc<-list("aftereight","lindt") 
gum<-list("trident","clortes") 
sweets<-list(gum,juices,energydrinks) 

almonds<-list("salted","roasted") 
pistcio<-list("flavourd","roasted") 
nuts<-list(almonds,pistcio) 

all_products<-list(sweets,nuts,drinks)

choc<-  
tabsetPanel(
tabPanel("aftereight"),
tabPanel("lindt")
)
bisc<-  
tabsetPanel(
tabPanel("loacker"),
tabPanel("tuc")
)
gm<-  
tabsetPanel(
tabPanel("trident"),
tabPanel("clortes")
)

hdrinks<-  
tabsetPanel(
tabPanel("tea"),
tabPanel("green tea")
)
jcs<-  
tabsetPanel(
tabPanel("orange"),
tabPanel("mango")
)
ngdrinks<-  
tabsetPanel(
tabPanel("powerhorse"),
tabPanel("redbull")
)

al<-  
tabsetPanel(
tabPanel("salted"),
tabPanel("roasted")
)
pst<-  
tabsetPanel(
tabPanel("flavourd"),
tabPanel("roasted")
)

runApp(list(
ui = shinyUI( fluidPage(

sidebarLayout( 
  sidebarPanel(width = 2),      
  mainPanel(tabsetPanel(id='conditioned',
                        tabPanel("sweets",value=1,
                                 tabsetPanel(
                                   tabPanel("biscuits",
                                            tabsetPanel(bisc)),
                                   tabPanel("choc",
                                            tabsetPanel(choc)),
                                   tabPanel("gum",
                                            tabsetPanel(gm))
                                 )),
                        tabPanel("nuts",value=2,
                                 tabsetPanel(
                                   tabPanel("almonds",
                                            tabsetPanel(al)),
                                   tabPanel("pistcio",
                                            tabsetPanel(pst))
                                 )),

                        tabPanel("drinks",value=3,
                                 tabsetPanel(
                                   tabPanel("hotdrinks",
                                            tabsetPanel(hdrinks)),
                                   tabPanel("juices",
                                            tabsetPanel(jcs)),
                                   tabPanel("energydrinks",
                                            tabsetPanel(ngdrinks))

                                 ))
                        ))
  ))),

 server = function(input, output, session) {}
))

如您所见,这种方法太容易出错,在此先感谢您。

苏里曼
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) {}
))

完整解决方案

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)})
        })
      }
    })
  }
))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用R从现有定义3个级别的列中形成1列?

使用R中的预定义功能组合列表的每个级别的元素

将列表存储在 R 中的数组中时,如何避免额外级别的列表嵌套?

从R中任何级别的列表中删除空列表

如何在R中的复杂列表中组合不同级别的多个元素?

R Shiny - 旋转 tabPanel 和 onclick 功能

如何在 R 的数据框中创建具有三个级别的因子?

如何显示r中每个级别的所有值?

如何从R中少于2个唯一级别的数据框中删除列

在R中基于两个因子级别插入行

基于R中列内因子级别的层次偏好的子集数据帧

计算R中可变级别的组合

R Shiny:无法在不同的 tabPanel 中使用功能

如何从R / Shiny中的传单地图获取缩放级别?

在R中,如何基于另一个列表限制列表的列表?

如何在R中自动命名元素列表(基于元素的数量)

如何从R中的列表中删除功能?

制作包含R中级别的小列表的大列表

R中具有data.frame的多个级别的嵌套列表

在 R Shiny 中调用 tabPanel 时如何使用 lapply 或其他高阶函数

如何从R中的数据框中删除包含唯一因子级别的行?

如何计算R中int向量中不同级别的数量?

如何使用URL中具有多个级别的zip文件在R中创建数据框?

使用 renderUI 在 R Shiny 中插入额外的 tabPanel

使用 r 基于组中的条件聚合从级别自动生成列

在R Shiny中,如何自动显示图像而不是文本?

如何在 R 中绘制具有不同级别的可折叠树?

如何按 r 中特定分组变量级别的特定顺序排列数据行

如何在R中添加不同级别的列并获得其频率