繁体   English   中英

如何 select shiny DT 数据表中的最大单元数

[英]How to select max number of cells in shiny DT datatable

有没有办法 select 在 shiny DT 数据表中达到一定数量的单元格? 我说的是单元格而不是行。

library(shiny)
if (packageVersion('DT') < '0.1.3') devtools::install_github('rstudio/DT')
library(DT)
shinyApp(
  ui = fluidPage(
    fluidRow(
      h1('Client-side processing'),
      DT::dataTableOutput('x1')

    )
  ),
  server = function(input, output, session) {
    output$x1 = DT::renderDataTable(
      iris, server = FALSE,
      selection = list(mode = 'multiple',target="cell")
    )

  }
)

是的,在 Select 扩展的帮助下:

library(shiny)
library(DT)

callback <- c(
  "var tblID = $(table.table().node()).closest('.datatables').attr('id');",
  "var inputName = tblID + '_cells_selected:DT.cellInfo'",
  "",
  "table.on('select', function(e, dt, type, ix){",
  "  var selected = dt.cells({selected: true});",
  ## send selected cells to Shiny
  "  var indices = selected.indexes().toArray()", 
  "    .map(function(x){return {row: x.row+1, column: x.column};});",
  "  Shiny.setInputValue(inputName, indices);",
  ## deselect if more than 5 selected
  "  if(selected.count() > 5){",
  "    dt.cells(ix).deselect();",
  "  }",
  "});",
  "",
  # on deselect, also send selected cells to Shiny
  "table.on('deselect', function(e, dt, type, ix){",
  "  var selected = dt.cells({selected: true});",
  "  var indices = selected.indexes().toArray()", 
  "    .map(function(x){return {row: x.row+1, column: x.column};});",
  "  Shiny.setInputValue(inputName, indices);",
  "});"
)

ui <- fluidPage(
  br(),
  DTOutput("tbl"),
  br(),
  h3("Selected cells:"),
  verbatimTextOutput("selectedCells")
)

server <- function(input, output, session){

  output[["tbl"]] <- renderDT({
    datatable(
      iris[1:6,],
      selection = "none",
      extensions = "Select",
      callback = JS(callback),
      options = list(
        select = list(style = "multi", items = "cell")
      )
    )
  })

  output[["selectedCells"]] <- renderPrint({
    input[["tbl_cells_selected"]]
  })

}

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM