闪亮:动态数量的数据表

4
我正在使用附加的代码来生成基于组的子表。由于某种原因,每个表格只呈现数据的最后一部分。 如果有人能告诉我出了什么问题就太好了。
BR

enter image description here

library(shiny)
library(shinydashboard)
library(DT)
library(data.table)

tabnames <- LETTERS[1:6]

DT <- data.table(mtcars[1:30,], keep.rownames=TRUE)
DT[, grp:=rep(tabnames, each=trunc(nrow(mtcars)/length(tabnames)))]


ui = dashboardPage(
  dashboardHeader(title = "Dynamic DTs"),
  dashboardSidebar(),
  dashboardBody(
    uiOutput("tables"),
    p(),
    verbatimTextOutput("selectedCells")
  )
  )


server <- function(input, output, session) {
  output$tables <- renderUI({
    output_list <- list()
    for(i in seq(tabnames)){
      output_list[[i]] <- column(4, DT::dataTableOutput(outputId=tabnames[i]))
    }
    print(fluidRow(output_list))
    return(fluidRow(output_list))
  })


  for(i in seq(tabnames)){
    tabname <- tabnames[i]
    local({
      print(DT[grp %in% tabname, 1:3])
      output[[tabname]] <- DT::renderDataTable({
        DT[grp %in% tabname, 1:3]
      }, selection=list(mode="multiple", target="cell"))
    })
  }
  output$selectedCells <- renderPrint(input$A_cells_selected)
}

shinyApp(ui = ui, server = server)
1个回答

5
好的,我找到了一个解决方案:需要将其传递到一个单独的变量中。
library(shiny)
library(shinydashboard)
library(DT)
library(data.table)

tabnames <- LETTERS[1:6]

DT <- data.table(mtcars[1:30,], keep.rownames=TRUE)
DT[, grp:=rep(tabnames, each=trunc(nrow(mtcars)/length(tabnames)))]


ui = dashboardPage(
  dashboardHeader(title = "Dynamic DTs"),
  dashboardSidebar(),
  dashboardBody(
    uiOutput("tables"),
    p(),
    verbatimTextOutput("selectedCells")
  )
  )


server <- function(input, output, session) {
  output$tables <- renderUI({
    output_list <- list()
    for(i in seq(tabnames)){
      output_list[[i]] <- column(4, DT::dataTableOutput(outputId=tabnames[i]))
    }
    print(fluidRow(output_list))
    return(fluidRow(output_list))
  })


  for(i in seq(tabnames)){
    tabname <- tabnames[i]
    local({
      subDT <- DT[grp %in% tabname, 1:3]
      output[[tabname]] <- DT::renderDataTable({
        subDT
      }, selection=list(mode="multiple", target="cell"))
    })
  }
  output$selectedCells <- renderPrint(input$A_cells_selected)
}

shinyApp(ui = ui, server = server)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接