如何一次显示无用表数量(结果不同)?闪亮的应用程序

时间:2019-09-11 11:46:14

标签: r shiny

根据我的选择,显示反应性表的数量有问题。我想做的是在一列中呈现尽可能多的表及其唯一记录,并一一显示每个子组的结果。

如果我分享此示例,将会更容易:


  dashboardHeader(title = "My App"),

  dashboardSidebar(id="", sidebarMenu(

    menuItem(strong("Tab1"), tabName = "T1", icon = icon("table")))),

  dashboardBody(

    tabItems(

      tabItem(
        tabName="T1",
              fluidRow(tableOutput('tables'))
        )
      )
    )
  )



server <- function(input, output) {

  observeEvent(length(unique(mtcars$gear)), {
    lapply(1:length(unique(mtcars$gear)), 
           function(i){
             output[[paste0('table', i)]]<-renderTable({

               filtered<-mtcars

               a<-list()

               for (j in unique(filtered$gear)){

                 subd <- filtered[filtered$gear == j,] 

                 a[[j]]<-subd

                 }

               for(i in 1:length(a)){
                 a[[i]]
               }

              a[[3]]

             })
           })
  })

  output$tables <- renderUI({
    lapply(1:length(unique(mtcars$gear)), 
           function(i) {
             uiOutput(paste0('table', i))
           })
  })


}

shinyApp(ui = ui, server = server)

我想从这段代码中得到的是3个表(一个表汇总为gear = 3,一个汇总表为gear = 4,一个汇总表为gear = 5)。我现在得到的是3个表,结果为gear = 3。

我试图编写一个遍历列表的for循环,但是我不知道应该在哪里添加此for循环以使其正常工作。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用split根据数据框中某个因子的级别创建表列表。从那里开始,只需对renderUI进行一些修改,即可获得所需的结果。还要注意,我将tables的输出更改为uiOutput而不是tableOutput,因为您用renderUI填充了该数据。

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "My App"),

  dashboardSidebar(id = "", sidebarMenu(
    menuItem(strong("Tab1"), tabName = "T1", icon = icon("table"))
  )),

  dashboardBody(tabItems(tabItem(
    tabName = "T1",
    fluidRow(uiOutput('tables'))
  )))
)

server <- function(input, output) {

  tables_in_list <- split(x = mtcars, f = mtcars$gear)

  output$tables <- renderUI({
    lapply(seq_along(tables_in_list), function(i)
      renderTable(tables_in_list[[i]]))
  })
  }

shinyApp(ui = ui, server = server)
相关问题